javascript .val() 没有从输入中获取更新值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15236703/
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
.val() not getting updated value from input
提问by LandonWO
I have two input fields, and I'm trying to get their values using jquery by clicking a button. It seems like a very simple operation, but for the life of me I can't get it to work. Here is the code snippet:
我有两个输入字段,我试图通过单击按钮使用 jquery 获取它们的值。这似乎是一个非常简单的操作,但对于我的生活,我无法让它发挥作用。这是代码片段:
Name: <input type="text" id="name" value="test1"><br>
Message: <input type="text" id="line" value="test2"><br>
<button id="submitButton">Submit</button>
<script>
name1 = $("#name").val();
line1 = $("#line").val();
$("#submitButton").click(function(){
alert("Name: " + name1 + "\nMessage: " + line1);
});
</script>
If I don't specify the value, the alert returns with undefined for both variables. If I change the values of the input and click the button, it still says test1 and test2. There has to be something simple that I'm missing here, anyone know what it is?
如果我不指定该值,警报将返回两个变量的 undefined。如果我更改输入的值并单击按钮,它仍然显示 test1 和 test2。必须有一些简单的东西我在这里失踪了,有人知道它是什么吗?
回答by chris-mv
In your code, you fetch the values when the script is initialized, and then reference the value they had at that point in your click function rather than fetching the current value.
在您的代码中,您在脚本初始化时获取值,然后在单击函数中引用它们当时的值,而不是获取当前值。
Try:
尝试:
$("#submitButton").click(function(){
name1 = $("#name").val();
line1 = $("#line").val();
alert("Name: " + name1 + "\nMessage: " + line1);
});
回答by Niet the Dark Absol
As mentioned in other answers, the problem is that you are precomuting the variables and expecting them to magically change. However I would strongly suggest using Vanilla JS
正如其他答案中提到的,问题在于您正在预先计算变量并期望它们神奇地改变。但是我强烈建议使用Vanilla JS
document.getElementById('submitButton').onclick = function() {
var name1 = document.getElementById('name').value,
line1 = document.getElementById('line').value;
alert("Name: " + name1 + "\nMessage: " + line1);
};
回答by Explosion Pills
name1 = $("#name").val()
creates a new string variable name1
that has the value of $("#name").val()
as it is at the moment. .val()
does not get called each time you try to access name1
. The simplest thing to do would just be to use $("#name").val()
instead.
创建一个新的字符串变量name1
,其值为$("#name").val()
当前。 .val()
每次尝试访问时都不会被调用name1
。最简单的方法就是使用$("#name").val()
。
回答by highwingers
you should wrap your code within document.ready() event...
您应该将代码包装在 document.ready() 事件中...
$(document).ready(function() {
name1 = $("#name").val();
line1 = $("#line").val();
$("#submitButton").click(function(){
alert("Name: " + name1 + "\nMessage: " + line1);
});
// Handler for .ready() called.
});
回答by nemmy
You've set variable values outside of the click function. To the value is set on page load, not whenever you run the function. Try this:
您已经在 click 函数之外设置了变量值。该值是在页面加载时设置的,而不是在您运行该函数时设置的。试试这个:
Name: <input type="text" id="name" value="test1"><br>
Message:
Submit
留言:
提交