繁体   English   中英

如何使用jquery更改变量中输入框的值?

[英]How do I change the value of an input box in a variable using jquery?

我有一个包含带有输入框的表单的变量,我想使用jquery更改所述输入框的value属性。 到目前为止,我可以更改值,但是在外部html中不会更改它。 这是到目前为止我得到的:

$("a").click(function(event) {
    var form_content = "<form><input id=\"name\" value=\"Test value\"/></form>";
    var test = $('<div />').html(form_content).find('#name').val('New test value').parent();
    alert(test.find('#name').val());
    alert(test.html());
    return false;
});

也可以在jsfiddle上获得 第一个alert显示"New test value" ,但是第二个alert未显示有关此新值如何存储的任何提示-基本上,它返回与原始变量form_content相同的值。

我在这里看了一下 ,但不明白我在做什么错。

谁能帮我把点子联系起来,并可能提示我如何更改form_content存储的html? (似乎有很多使用纯静态html的示例,但是不幸的是,这不是我的选择。)谢谢!

$("a").click(function(event) {
    var form_content = "<form><input id=\"name\" value=\"Test value\"/></form>";
    var test = $('<div />').html(form_content).find('#name').attr('value', 'New test value').parent();
    alert(test.find('#name').attr('value'));
    alert(test.html());
    return false;
});

更新您的查询,它的工作正常...

不要使用.val('新测试值'),而应使用.attr('value','新测试值')

只需为您的类型输入元素,尝试

      <input type="hidden" id="name" value="Test value">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM