Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

javascript - 警告变量值(alert a variable value)

How do I display the value of a variable in javascript in an alert box?

(如何在警告框中的javascript中显示变量的值?)

For example I've got a variable x=100 and alert(x) isn't working.

(例如,我有一个变量x = 100而alert(x)不起作用。)

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)

  }
}
  ask by technocrat translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A couple of things:

(有几件事:)

  1. You can't use new as a variable name, it's a reserved word.

    (你不能使用new作为变量名,它是一个保留字。)

  2. On input elements, you can just use the value property directly, you don't have to go through getAttribute .

    (在input元素上,您可以直接使用value属性,而不必通过getAttribute 。)

    The attribute is "reflected" as a property.

    (该属性被“反映”为属性。)

  3. Same for name .

    (name相同。)

So:

(所以:)

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...