设置变量时jQuery .val()不起作用 [英] jQuery .val() not working when setting a variable
本文介绍了设置变量时jQuery .val()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有这样的输入:
<input type="text" id="textvalue" />
以下代码将更改其值:
$(document).ready(function() {
$('#textvalue').val("hello");
});
但是以下方法不起作用:
however the following will not work:
$(document).ready(function() {
var = "hello";
$('#textvalue').val(var);
});
第二个为什么不起作用?我需要能够将文本框的值更改为变量的值
Why does the second one not work? I need to be able to change the value of the textbox to the value of a variable
推荐答案
您的var
语句需要看起来像这样
Your var
statement needs to look something like this
var something = "hello"
$('#textvalue').val(something );
现在,您实际上没有为变量分配值,然后尝试使用var
关键字.
Right now your not actually assigning a value to a variable, and then you are trying to use the var
keyword.
Variable Reference
这篇关于设置变量时jQuery .val()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文