javascript 按名称获取复选框的值

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

Get value of checkbox by name

javascriptjqueryhtmljquery-mobile

提问by user1809790

I need to get the value of a checkbox by name. I have the following code:

我需要按名称获取复选框的值。我有以下代码:

<input type="hidden" name="my_checkbox" value="0">
<input type="checkbox" name="my_checkbox" id="my_checkbox_id" value="1"  />

The idea is that if the checkbox is not checked, I would get the value of the input type hidden (which is 0).

这个想法是,如果未选中复选框,我将获得隐藏的输入类型的值(即 0)。

Here is what I have tried but did not work:

这是我尝试过但没有奏效的方法:

$('input[name=my_checkbox]').val();

However this returned the value of both the hidden input and checkbox. Can anyone tell me how I can get the value of the checkbox when checked (1) or else get the value of the hidden input (0) when unchecked? I guess this has to be done by name so that if the checkbox is unchecked, I get the value of the hidden input as if the checkbox is unchecked you won't get its value.

然而,这返回了隐藏输入和复选框的值。谁能告诉我如何在选中 (1) 时获取复选框的值,或者在未选中时获取隐藏输入 (0) 的值?我想这必须按名称完成,以便如果未选中复选框,我将获得隐藏输入的值,就像未选中复选框一样,您将不会获得其值。

回答by Arslan Butt

<input type="hidden" name="my_checkbox" value="0">
<input type="checkbox" name="my_checkbox" id="my_checkbox_id" value="1"  />

Try this it worked for me:

试试这个它对我有用:

$( 'input[name="my_checkbox"]:checked' ).val();

回答by ATOzTOA

Why don't you do this:

你为什么不这样做:

if($("#my_checkbox_id").is(':checked')) {
    output = $("#my_checkbox_id").val();
} else {
    output = $('input[type=hidden]').val();
}

回答by Jai

just find the :visiblecheckbox:

只需找到:visible复选框:

$('input[name="my_checkbox"]:visible').change(function() {
  if ($('input[name="my_checkbox"]:visible').is(':checked')) {
    alert($('input[name="my_checkbox"]:visible').val());
  } else {
    alert($('input[name="my_checkbox"]:hidden').val());
  }
});

tryout the fiddle: http://jsfiddle.net/JgzGa/1/

试用小提琴:http: //jsfiddle.net/JgzGa/1/

回答by Roko C. Buljan

Actually you don't need the hidden one, you just need to say:
if checkbox is not checked set his value to '0'
and this is all you need:

其实你不需要隐藏的,你只需要说:
if checkbox is not checked set his value to '0'
这就是你所需要的:

$('input:checkbox').not(':checked').val(0);

回答by Matti Virkkunen

You could add a new function for it:

您可以为它添加一个新功能:

$.fn.checkboxVal = function() {
    var checked = $(this).filter("[type=checkbox]:checked");
    if (checked.length)
        return checked.val();

    return $(this).filter("[type=hidden]").val();
};

Then you can say $("input[name=foo]").checkboxVal()and it'll get you the correct value.

然后你可以说$("input[name=foo]").checkboxVal(),它会给你正确的价值。

Untested, so fixing any bugs is left as an exercise for the reader.

未经测试,因此修复任何错误留给读者作为练习。