jQuery val()和复选框 [英] jQuery val() and checkboxes

查看:55
本文介绍了jQuery val()和复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

val()是否对复选框控件没有用,但是对于在其他所有输入控件中一致地获取输入数据很有用?

Is there some rationale for val() being useless for checkbox controls, whereas it is useful for getting input data consistently across every other input control?

例如对于复选框,无论复选框状态如何,在处始终会显示打开".对于实际上没有值"属性的其他输入控件,例如选择和textarea,它的行为与您期望的一样.参见:

e.g. for checkboxes, at appears to always return "on" regardless of the checkbox state. For other input controls that don't actually have a "value" attribute, e.g. select and textarea, it behaves as you would expect. See:

http://jsfiddle.net/jamietre/sdF2h/4/

我想不出为什么它不会返回truefalse的充分理由.如果失败,则至少在选中时至少返回"on",而在未选中时则返回空字符串.如果复选框没有value属性,则至少失败总是返回一个空字符串!

I can't think of a good reason why it wouldn't return true or false. Failing that, at least return "on" only when checked, and an empty string when not. Failing that, at least always return an empty string, given that checkboxes have no value attribute!

很明显,我知道如何使用attr来获取值,但这就是这种情况.我正在开发一个简单的(到目前为止)C#jQuery实现以在服务器上进行HTML解析,并且我试图完全忠实于jQuery的实现,以便它在客户端或服务器上针对相同的DOM表现一致.但这似乎很愚蠢,无论如何,我都很难让自己实际编写"value"以使复选框返回"ON".但是,如果我不同意,那将是不一致的.所以我想了解一下,这样做有什么理由吗?它是出于某种目的,还是仅仅是某种人工制品?有人会对复选框使用val()方法吗,如果是这样,为什么?如果不是,为什么jQuery架构师决定采用这种方法以使其无效?

Obviously I know how to get the value using attr, but here's the situation. I am developing a simple (so far anyway) C# jQuery implementation to do HTML parsing on the server, and I am trying to be completely faithful to jQuery's implementation so it behaves consistently on either the client or server against the same DOM. But this just seems stupid and I'm having a hard time getting myself to actually code "value" to return "ON" for a checkbox no matter what. But if I don't, it won't be consistent. So I'm trying to understand, is there some reason for doing this? Does it serve some purpose, or is it simply an artifact of some kind? Would anyone ever use the val() method against a checkbox, if so, why? If not, why did the jQuery architects decide on this approach to make it not useful?

推荐答案

我想不出一个很好的理由 不会返回true或false.失败 至少在以下情况下才返回"on" 检查,如果没有则为空字符串. 失败了,至少总是返回 一个空字符串,考虑到复选框 没有值属性!

I can't think of a good reason why it wouldn't return true or false. Failing that, at least return "on" only when checked, and an empty string when not. Failing that, at least always return an empty string, given that checkboxes have no value attribute!

复选框确实具有值属性.在例如中使用它是很常见的.表单中的多个复选框,例如:

Checkboxes do have a value attribute. It's quite common to use it in eg. multi-checkboxes in forms, for example:

<input type="checkbox" name="foo[]" value="1" />
<input type="checkbox" name="foo[]" value="2" />

然后,当用户提交表单时,服务器可以通过检查foo[]数组的值来解析检查了哪个复选框.无论是否选中该复选框,其值均不会更改;浏览器根本不会将未选中的复选框的值发送到服务器.如果你这样做. $('#your-form').serialize()在jQuery中,它应该类似地工作;它不应包含未选中的复选框的值.如果您忽略复选框中的值,则其默认值为on.

Then when user submits the form, the server can parse which of the checkboxes were checked by checking the values of the foo[] array. The value of the checkbox does not change whether it is checked or not; the browser simply does not send the values of unchecked checkboxes to the server. If you do eg. $('#your-form').serialize()in jQuery, it should work similarly; it shouldn't include values of unchecked checkboxes. If you omit the value from checkbox, it simply defaults to on.

如果要检查是否在jQuery中选中了单个复选框,最好的描述性最好的方法是$(this).is(':checked')

If you want to check whether an individual checkbox is checked or not in jQuery, the best most descriptive way is to do $(this).is(':checked')

这篇关于jQuery val()和复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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