我有一个带有复选框的相当简单的表单,我注意到我的复选框值没有在我的服务器端应用程序中被拾取:
$.post('CreateForm', $('#new-form').serialize(), ...
其他一切都正确发布,但我在 Firebug 中看到它会将复选框值序列化为“on”或“off”,而不是我使用普通 <form method="post" action="formpage">
获得的“true”和“false” ...这是怎么回事,这是预期的行为吗?我的服务器端模型联编程序不将“on”等同于“true”,因此会降低该值。显然我可以更改模型 Binder ,但我想确保我没有做错。
编辑:
这是我的标记:
<input type="checkbox" name="CheckboxValue" >
jQuery 版本 1.4.4
在 Firebug 中,这是生成的帖子:
... other variables &CheckboxValue=on
最佳答案
问题是您的复选框没有value
属性。当 jQuery 序列化表单时,它使用 'on' 作为默认值。
你应该给你的复选框一个值:
<input type="checkbox" name="CheckboxValue" value="true" />
请参阅此处示例:http://jsfiddle.net/U6Sbw/1/
关于javascript - jQuery 表单后将 bool 复选框转换为 "on"和 "off"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5317664/