Javascript 提交表单时,HTML 输入复选框返回“开”而不是“真”

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

HTML Input Checkbox return 'On' instead of 'True' when submitting form

javascriptjqueryhtml

提问by user460667

I have a MVC3 app using Project Awesome (http://awesome.codeplex.com/), but I am getting a weird behaviour on checkboxes. I have the following simple Html within a Modal popup <input type="checkbox" class="check-box" name="IsDeleted">

我有一个使用 Project Awesome (http://awesome.codeplex.com/) 的 MVC3 应用程序,但是我在复选框上出现了奇怪的行为。我在模态弹出窗口中有以下简单的 Html<input type="checkbox" class="check-box" name="IsDeleted">

When I submit the form containing this element, its post value is 'on' instead of the expected 'true' (when element is checked).

当我提交包含此元素的表单时,它的 post 值为“on”而不是预期的“true”(当元素被选中时)。

Does anybody know why this is? I am assuming there may be some javascript somewhere messing with the form data, but wanted to check whether there isn't some HTML I am missing.

有人知道这是为什么吗?我假设某处可能有一些 javascript 弄乱了表单数据,但想检查是否没有我遗漏的一些 HTML。

Thanks

谢谢

回答by Niklas

Set the checkboxes valueattribute to trueand you will get truein your post value.

将复选框value属性设置为true,您将获得true您的帖子值。

回答by mkilmanas

It's browser specific, I suppose, what to send when value is undefined. You need to defined valueattribute on your radios/checkboxes to be sure what will be passed back to you. I would suggest value="1"

我想这是浏览器特定的,当值未定义时发送什么。您需要value在收音机/复选框上定义属性,以确保将传递回给您的内容。我会建议value="1"

回答by Otto Kanellis

set data-val="true" and value="true"by deafult... if checkbox is checked then returns true

data-val="true" and value="true"由默认设置...如果复选框被选中则返回 true

回答by Michael Wright

Surely you should just check if it is set - the value that it sends across is irrelevant, if it's not checked, then nothing at all gets sent when you POST.

当然,您应该只检查它是否已设置 - 它发送的值无关紧要,如果未检查,则在您 POST 时根本不会发送任何内容。

回答by Siddhartha

Check Checkbox is checked or not if checked set Hidden field true else set hidden field false.

如果选中设置隐藏字段为真,否则设置隐藏字段为假。

$('#hiddenFieldId').val($('#CheckBoxId').attr('checked')=='checked')

回答by Trang Tran Thi My

In HTML, add a input type="hidden" above checkbox:

在 HTML 中,在复选框上方添加 input type="hidden":

<input name="active" id="activeVal" type="hidden">
<input id="active" type="checkbox">

Then, add a script as below:

然后,添加如下脚本:

$('#activeVal').val($('#active').is(':checked'));
$('#active').change(function() {
     $('#activeVal').val($('#active').is(':checked'));
});

When you do eg. $('#your-form').serialize()in jQuery, you will get value of checkbox when checked active: trueor uncheck active: false

当你做例如。$('#your-form').serialize()在 jQuery 中,选中active: true或取消选中时,您将获得复选框的值active: false

回答by Venkat

Use jQuery

使用 jQuery

var out=$('.check-box').is('checked')

If checkbox is checked out=true else out=false

如果复选框被选中=true 否则 out=false

回答by sergzach

Use jQuery for cross-browser decision. And it will return trueor falseanyway.

使用 jQuery 进行跨浏览器决策。无论如何它都会返回

$('#check-box-id').attr('checked' ) == true

if your checkbox has an id check-box-id. For your current version use the next(select by class name):

如果您的复选框有一个 id check-box-id。对于您当前的版本,请使用下一个(按类名选择):

$('.check-box').attr('checked' ) == true