简体   繁体   English

如何获取所选的复选框值?

[英]How to get the selected checkbox value?

I am trying to get the selected checkboxes value and the hidden field value next to it when user submits the form. 我试图在用户提交表单时获取选中的复选框值和旁边的隐藏字段值。

<form....>

<td><input type='checkbox' name='checkbox' value='1' ></input></td>
<input type='hidden' name='sid1' value='1'/>

<td><input type='checkbox' name='checkbox' value='1' ></input></td>
<input type='hidden' name='sid2' value='2'/>

<td><input type='checkbox' name='checkbox' value='1' ></input></td>
<input type='hidden' name='sid3' value='3'/>

I try: 我尝试:

$(document).ready(function(){
    if( $("input[name='checkbox']['checked']")) {
        $("input[name='checkbox']['checked']").each(function(){ 
            var test=$(this).val();
            alert(test);    
        })
    }
})

but it will gives me all the checkbox value even thought they were not checked. 但它会给我所有的复选框值,即使他们没有被检查。 Any one could help me about this? 任何人都可以帮我这个吗? Thanks a lot! 非常感谢!

Here it is: 这里是:

<HTML>
   <HEAD>
      <TITLE>Multiple-Select Check Boxes</TITLE>
      <SCRIPT LANGUAGE="JavaScript">
         function getSelected(opt) {
            var selected = new Array();
            var index = 0;
            for (var intLoop = 0; intLoop < opt.length; intLoop++) {
               if ((opt[intLoop].selected) ||
                   (opt[intLoop].checked)) {
                  index = selected.length;
                  selected[index] = new Object;
                  selected[index].value = opt[intLoop].value;
                  selected[index].index = intLoop;
               }
            }
            return selected;
         }

         function outputSelected(opt) {
            var sel = getSelected(opt);
            var strSel = "";
            for (var item in sel)       
               strSel += sel[item].value + "\n";
            alert("Selected Items:\n" + strSel);
         }
      </SCRIPT> 
   </HEAD>
   <BODY> 
      <FORM NAME="ColorSelector">
         <INPUT TYPE=CHECKBOX NAME="color" VALUE="Red">Red
         <INPUT TYPE=CHECKBOX NAME="color" VALUE="Navy" CHECKED>Navy
         <INPUT TYPE=CHECKBOX NAME="color" VALUE="Black">Black
         <INPUT TYPE=CHECKBOX NAME="color" VALUE="White" CHECKED>White
         <INPUT TYPE=BUTTON VALUE="Selected Check Box Items" 
            ONCLICK="outputSelected(this.form.color);">
         <P>
         <SELECT NAME="multistore" SIZE=3 MULTIPLE>
            <OPTION VALUE="Computer" SELECTED>Computer</OPTION>
            <OPTION VALUE="Bookstore">Book Store</OPTION>
            <OPTION VALUE="MailOrder" SELECTED>Mail Order</OPTION>
         </SELECT>
         <INPUT TYPE=BUTTON VALUE="Selected List Items" 
            ONCLICK="outputSelected(this.form.multistore.options)">
      </FORM>
   </BODY>
</HTML>

['checked']不是有效的选择器,我认为您正在寻找:已选中

$("input[name='checkbox']:checked")

First of all, you're using the exact same name for your input elements. 首先,您使用输入元素的完全相同的名称。 You should do something like: 你应该做的事情如下:

<input type='checkbox' name='checkbox[]' value='1'/>
<input type='checkbox' name='checkbox[]' value='2'/>
<input type='checkbox' name='checkbox[]' value='3'/>

To select all inputs which are checked, use: 要选择所有已检查的输入,请使用:

$('input:checked').each(function() {
   // To pass this value to its nearby hidden input
   $(this).parent('td').next('input').val(this.value);
});

You want this instead: 你想要这个:

$("input[type='checkbox']:checked")
                         ^^^^^^^^^---use this

You may try this too 你也可以尝试一下

    $("input:checked").each(function(){ 
        var test=$(this).val();
        alert(test);    
    });

Fiddle here . 在这里小提琴

//Script    
<script type="text/javascript">
            function getCheck() {
                alert(document.getElementById('<%= CheckBox1.ClientID %>').checked);
            }

    </script>
//Form 
        <form id="form1" runat="server">
        <div>

            <asp:CheckBox ID="CheckBox1" runat="server" />

            <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="getCheck()" />

        </div>
        </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM