警告变量值 [英] alert a variable value

查看:105
本文介绍了警告变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在警告框中的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)

  }
}


推荐答案

一些事情:


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

  2. 输入元素,你可以直接使用属性,您不必通过 getAttribute 。该属性被反映为属性。

  3. 名称

  1. You can't use new as a variable name, it's a reserved word.
  2. On input elements, you can just use the value property directly, you don't have to go through getAttribute. The attribute is "reflected" as a property.
  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆