jQuery表单发布将布尔值复选框转换为"on".和“关闭"? [英] jQuery form post converts boolean checkboxes to "on" and "off"?

查看:82
本文介绍了jQuery表单发布将布尔值复选框转换为"on".和“关闭"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的带有复选框的表单,并且我注意到我的复选框值未在服务器端应用程序中获取:

I have a fairly simple form with a checkbox, and I noticed that my checkbox values weren't being picked up on my server side app:

$.post('CreateForm', $('#new-form').serialize(), ...

其他所有内容均正确发布,但是我在Firebug中看到它将序列化复选框值的顺序为"on"或"off",而不是正常的<form method="post" action="formpage">所获得的"true"和"false". ..这是怎么回事,这是预期的行为吗?我的服务器端模型绑定程序没有将"on"等同于"true",因此降低了该值.显然,我可以更改模型联编程序,但想确保自己没有做错事.

Everything else posts correctly, but I'm seeing in Firebug that it'll serialize the checkbox value as "on" or "off" instead of "true" and "false" that I get with a normal <form method="post" action="formpage"> ... what's going on here, is this expected behavior? My server side model binder doesn't equate "on" with "true" and thus drops the value. Obviously I could change the model binder, but wanted to make sure I wasn't doing it wrong.

这是我的标记:

<input type="checkbox" name="CheckboxValue" >

jQuery版本1.4.4

jQuery version 1.4.4

在Firebug中,结果如下:

In Firebug here's the resulting post:

... other variables &CheckboxValue=on

推荐答案

问题是您的复选框没有value属性. jQuery序列化表单时,它使用"on"作为默认值.

The problem is that your checkboxes do not have value attributes. When jQuery serializes the form, it uses 'on' as the default value.

您应该为您的复选框指定一个值:

You should give your checkbox a value:

<input type="checkbox" name="CheckboxValue" value="true" />

在此处查看示例: http://jsfiddle.net/U6Sbw/1/

这篇关于jQuery表单发布将布尔值复选框转换为"on".和“关闭"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆