Jquery 更改隐藏表单字段值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3696272/
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
Jquery change hidden form field value
提问by Nick Craver
I have this code but it isn't working on my localhost and I don't know why. Can anyone shed any light on this?
我有这个代码,但它在我的本地主机上不起作用,我不知道为什么。任何人都可以对此有所了解吗?
(Script is in head and the other code is in the body)
(脚本在头部,其他代码在主体中)
<script type="text/javascript">
$(function(){
$(input[name='jsenabled']).val('1');
});
</script>
<input type="hidden" name="jsenabled" value="0" />
<label for="signup-email">Sign up for email offers, news & events:</label>
<input type="text" name="signup-email" id="signup-email" />
<input type="submit" id="signup-button" value="Sign Me Up!" />
<p id="signup-response"></p>
</fieldset>
EDITED Weird thing is I have added this code to test if the value has been changed and it does fire up an alert, but firebug doesn't register the change.
编辑 奇怪的是我添加了此代码来测试值是否已更改并且它确实会触发警报,但萤火虫没有注册更改。
$("input[name='jsenabled']").val("1");
if($("input[name='jsenabled']").val = '1') {
alert('frf');
}
回答by álvaro González
jQuery selectors are strings: they must be quoted
jQuery.val() is a method, not a property. Don't forget the parenthesis.
The comparison operator is
==
not=
.
jQuery 选择器是字符串:它们必须被引用
jQuery.val() 是一个方法,而不是一个属性。不要忘记括号。
比较运算符
==
不是=
。
You basically need to be more careful with your syntax.
您基本上需要更加小心您的语法。
回答by Nikita Rybak
Try $("input[name='jsenabled']").val('1');
Pay attention at double quotes I added.
尝试$("input[name='jsenabled']").val('1');
注意我添加的双引号。
editSee more issues in Nick's and Alvaro's answers.
编辑在尼克和阿尔瓦罗的回答中查看更多问题。
回答by Nick Craver
You need quotes around the selector, like this:
您需要在选择器周围使用引号,如下所示:
$(function(){
$("input[name='jsenabled']").val('1');
});
Also when checking the value, use parenthesis, like this:
同样在检查值时,请使用括号,如下所示:
if($("input[name='jsenabled']").val() == '1') {
alert('frf');
}
.val()
is a function, and called without any arguments getsthe value.
.val()
是一个函数,不带任何参数调用获取值。
On a more broad note, pay attention to the console, as Firebug should be throwing your JavaScript errors here for each of these, the console (which shows errors) is your best resource here.
更广泛地说,请注意控制台,因为 Firebug 应该在此处为每个错误抛出您的 JavaScript 错误,控制台(显示错误)是您最好的资源。
回答by Hulk
Can u try this $('input[name="jsenabled"]').val('1');
你能试试这个吗 $('input[name="jsenabled"]').val('1');
回答by acid
álvaro is generally right, but even with the right syntax you wouldn't be able to change the value of hidden fields, though jQuery sees them as readonly, if I recognize right.
álvaro 通常是正确的,但即使使用正确的语法,您也无法更改隐藏字段的值,尽管 jQuery 将它们视为只读,如果我认识正确的话。
I'd met this problem for myself a few weeks ago, my solution was a normal input with the css class 'hidden'
几周前我自己遇到了这个问题,我的解决方案是使用 css 类“隐藏”的正常输入
.hidden {
display: none;
}
afterwards, you could simply change the value of the hidden input field:
之后,您可以简单地更改隐藏输入字段的值:
jQuery('#field_id.hidden').val('new_field_value');