javascript 将隐藏表单字段的值设置为“false”但在服务器端设置为“true”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4798295/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:55:03  来源:igfitidea点击:

Setting hidden form field's value to 'false' yet getting 'true' on server side

javascriptjquery

提问by Blankman

I have a hidden form field:

我有一个隐藏的表单字段:

<input type=hidden id=blah1 value=true />

I have abutton, when clicked I do:

我有一个按钮,当点击我做:

$("#b1").bind("click", function(){

$("#blah1").attr("value", "false");

});

But when I get the form value on the server side, it is 'true'.

但是当我在服务器端获取表单值时,它是“true”。

Am I doing something wrong?

难道我做错了什么?

I even did this:

我什至这样做了:

e.preventDefault();
$("#blah1").attr("value", "false");
alert( $("#blah1").attr("value") );

It alerted the value 'false'.

它警告值“假”。

采纳答案by jpwco

You did include the 'name' attribute in your real code, no?

您确实在实际代码中包含了“名称”属性,不是吗?

<input type=hidden id=blah1 value=true name=blah1 />

This still doesn't explain why you see "true" on the server side. If you did include the "name" attribute on your hidden field, please post your server-side code.

这仍然不能解释为什么您在服务器端看到“true”。如果您确实在隐藏字段中包含了“名称”属性,请发布您的服务器端代码。

Running the code you have posted here, once the "name" attribute is added, achieved the expected "False" result.

运行您在此处发布的代码,一旦添加了“name”属性,就会达到预期的“False”结果。

回答by user2175189

Even aside from there being no 'name' attribute, you are setting the value to "false" as in a string, and a non-empty string will return true EVERY TIME.

即使除了没有 'name' 属性之外,您也将值设置为“false”,就像在字符串中一样,并且非空字符串每次都会返回 true。

$("#blah1").attr("value", "false");

should instead be

应该是

$("#blah1").attr("value", false);