警告变量值 [英] alert a variable value
本文介绍了警告变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在警告框中的javascript中显示变量的值?
How do I display the value of a variable in javascript in an alert box?
例如,我有一个变量x = 100和alert(x)不工作。
For example I've got a variable x=100 and alert(x) isn't working.
油脂猴子使用的脚本在这里
the script used in grease monkey is here
var inputs = document.getElementsByTagName('input');
var new;
for (i=0; i<inputs.length; i++) {
if (inputs[i].getAttribute("name") == "ans") {
new=inputs[i].getAttribute("value"));
alert(new)
}
}
推荐答案
一些事情:
- 你不能使用
new
作为变量名,它是一个保留字。 - 在
输入
元素,你可以直接使用值
属性,您不必通过getAttribute
。该属性被反映为属性。 -
名称
。
- You can't use
new
as a variable name, it's a reserved word. - On
input
elements, you can just use thevalue
property directly, you don't have to go throughgetAttribute
. The attribute is "reflected" as a property. - Same for
name
.
所以:
var inputs, input, newValue, i;
inputs = document.getElementsByTagName('input');
for (i=0; i<inputs.length; i++) {
input = inputs[i];
if (input.name == "ans") {
newValue = input.value;
alert(newValue);
}
}
这篇关于警告变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文