设置变量时jQuery .val()不起作用 [英] jQuery .val() not working when setting a variable

查看:1070
本文介绍了设置变量时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屋!

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