简体   繁体   中英

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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