Javascript 警告变量值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2983288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
alert a variable value
提问by technocrat
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)
}
}
采纳答案by T.J. Crowder
A couple of things:
几件事:
- You can't use
newas a variable name, it's a reserved word. - On
inputelements, you can just use thevalueproperty directly, you don't have to go throughgetAttribute. The attribute is "reflected" as a property. - Same for
name.
- 您不能
new用作变量名,它是一个保留字。 - 在
input元素上,您可以直接使用该value属性,而不必经过getAttribute. 该属性被“反映”为一个属性。 - 对于
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);
}
}
回答by JosephDoggie
Note, while the above answers are correct, if you want, you can do something like:
请注意,虽然上述答案是正确的,但如果您愿意,您可以执行以下操作:
alert("The variable named x1 has value: " + x1);
回答by Coronus
If you're using greasemonkey, it's possible the page isn't ready for the javascript yet. You may need to use window.onReady.
如果您使用的是greasemonkey,则该页面可能还没有为javascript 做好准备。您可能需要使用 window.onReady。
var inputs;
function doThisWhenReady() {
inputs = document.getElementsByTagName('input');
//Other code here...
}
window.onReady = doThisWhenReady;
回答by Laxmi Priyam Dubey
var input_val=document.getElementById('my_variable');for (i=0; i<input_val.length; i++) {
xx = input_val[i];``
if (xx.name == "ans") {
new = xx.value;
alert(new); }}
回答by pradip kor
show alert box with use variable with message
显示带有使用变量和消息的警告框
<script>
$(document).ready(function() {
var total = 30 ;
alert("your total is :"+ total +"rs");
});
</script>
回答by Sean Tank Garvey
If I'm understanding your question and code correctly, then I want to first mention three things before sharing my code/version of a solution. First, for both nameand valueyou probably shouldn't be using the getAttribute()method because they are, themselves, properties of (the variable named) inputs(at a given index of i). Secondly, the variable that you are trying to alert is one of a select handful of terms in JavaScript that are designated as 'reserved keywords' or simply "reserved words". As you can see in/on this list (on the link), newis clearly a reserved word in JS and should never be used as a variable name. For more information, simply google 'reserved words in JavaScript'. Third and finally, in your alert statement itself, you neglected to include a semicolon. That and that alone can sometimes be enough for your code not to run as expected. [Aside: I'm not saying this as advice but more as observation: JavaScript will almost always forgive and allow having too many and/or unnecessary semicolons, but generally JavaScript is also equally if not moreso merciless if/when missing (any of the) necessary, required semicolons. Therefore, best practice is, of course, to add the semicolons only at all of the required points and exclude them in all other circumstances. But practically speaking, if in doubt, it probably will not hurt things by adding/including an extra one but will hurt by ignoring a mandatory one. General rules are all declarations and assignments end with a semicolon (such as variable assignments, alerts, console.log statements, etc.) but most/all expressions do not (such as for loops, while loops, function expressions Just Saying.] But I digress..
如果我正确理解您的问题和代码,那么在分享我的代码/解决方案版本之前,我想先提三件事。首先,对于这两者name,value您可能不应该使用该getAttribute()方法,因为它们本身就是(命名的变量)inputs(在给定索引处i)的属性。其次,您尝试提醒的变量是 JavaScript 中被指定为“保留关键字”或简称为“保留词”的少数几个术语之一。正如您在此列表中(在链接上)中看到的,new显然是 JS 中的保留字,不应用作变量名。欲了解更多信息,只需谷歌“JavaScript 中的保留字”。第三,也是最后,在你的 alert 语句中,你忽略了包含一个分号。仅此一项有时就足以让您的代码无法按预期运行。[旁白:我不是说这是作为建议,而是作为观察:JavaScript 几乎总是会原谅并允许有太多和/或不必要的分号,但一般来说,如果/当缺少分号(任何一个) 必要的,必需的分号。因此,最佳实践当然是仅在所有必需的点添加分号,并在所有其他情况下排除它们。但实际上,如果有疑问,添加/包含一个额外的内容可能不会有什么坏处,但忽略一个强制性的内容可能会造成伤害。一般规则是所有声明和赋值都以分号结尾(例如变量赋值、警报、console.log 语句等)但大多数/所有表达式都没有(例如 for 循环、while 循环、函数表达式 Just Saying。] 但是我离题了。。
function whenWindowIsReady() {
var inputs = document.getElementsByTagName('input');
var lengthOfInputs = inputs.length; // this is for optimization
for (var i = 0; i < lengthOfInputs; i++) {
if (inputs[i].name === "ans") {
var ansIsName = inputs[i].value;
alert(ansIsName);
}
}
}
window.onReady = whenWindowIsReady();
PS: You used a double assignment operator in your conditional statement, and in this case it doesn't matter since you are comparing Strings, but generally I believe the triple assignment operator is the way to go and is more accurate as that would check if the values are EQUIVALENT WITHOUT TYPE CONVERSION, which can be very important for other instances of comparisons, so it's important to point out. For example, 1=="1" and 0==false are both true (when usually you'd want those to return false since the value on the left was not the same as the value on the right, without type conversion) but 1==="1" and 0===false are both false as you'd expect because the triple operator doesn't rely on type conversion when making comparisons. Keep that in mind for the future.
PS:您在条件语句中使用了双重赋值运算符,在这种情况下,由于您正在比较字符串,因此无关紧要,但通常我相信三重赋值运算符是可行的方法,并且更准确,因为它会检查是否这些值是 EQUIVALENT WITHOUT TYPE CONVERSION,这对于其他比较实例可能非常重要,因此指出这一点很重要。例如, 1=="1" 和 0==false 都是真(通常你希望它们返回假,因为左边的值与右边的值不同,没有类型转换)但是1==="1" 和 0===false 都是假的,因为三元运算符在进行比较时不依赖于类型转换。记住这一点,以备将来使用。

