简体   繁体   English

如何使用jQuery获取按钮单击时所有选中复选框的ID

[英]How to get ids of all checked checkbox on button click using jQuery

<html>  
    <input type='checkbox' id='1' name='checkMr[]' value='1' />       
    <input type='checkbox' id='2' name='checkMr[]' value='2' />            
    <input type='checkbox' id='3' name='checkMr[]' value='3' />         
    <input type='checkbox' id='4' name='checkMr[]' value='4' />        

    <a id="savedoctorschedule" style="float: right;" class="clyes clbutton clbtnSave">Save</a>       
</html> 

<script>
$j("#savedoctorschedule").bind("click", function () { 
    $j.ajax({
        url: "schedulemr.php",
        type: "post",
        data: { schmrid: $j("#sel_mr").val(), 
        schedocid: $j("#checkMr[]").val(), 
        schedate: $j("#date1").val(), },
        success:function(response){         
        }
</script>

<?php
include '../includes/include.php';
$schmrid = $_POST['sel_mr'];
$schedate = $_POST['date1'];
$schedocid = $_POST['checkMr'];
foreach($schedocid as $a => $b)
{
   $sql="INSERT INTO tbl_mr_schedule(doctor_idscheduled_date) 
   VALUES ('".$schedocid[$a]."','".$schmrid."','0','".$date."','".$schedate."');";
   mysql_query($sql) or die(mysql_error());
}
header('Location:../visitplan/');
?>

I want all the ids of checked checkboxes using jQuery; 我想要使​​用jQuery的所有已选中复选框的ID; the checkbox may be n number. 该复选框可能是n号。 I want to insert the checked checkboxes' value in the database where the number of records will depend upon the number of checked checkbox. 我想将选中复选框的值插入数据库中,记录的数量将取决于选中复选框的数量。 This would be using PHP as server-side script. 这将使用PHP作为服务器端脚本。

How can I fix it? 我该如何解决?

Try below, 试试下面,

$(':checkbox[name="checkMr[]"]:checked') //returns you the list of selected checkbox

then you can, 那么你也能,

var selectedID = [];
$(':checkbox[name="checkMr[]"]:checked').each (function () {
    selectedID.push(this.id);
});

DEMO DEMO

Your document is invalid for several reasons. 您的文档无效有多种原因。 Skipping the obvious (missing doctype/ <body> /etc), the id attribute may not begin with a number; 跳过明显的内容(缺少doctype / <body> / etc),id属性可能不能以数字开头; id="1" is invalid. id="1"无效。 You'll have to use a non-numeric prefix like id="check-1" or by assigning the ID as the value of the checkbox. 您必须使用非数字前缀,例如id="check-1"或将ID分配为复选框的value

Once you've fixed this, you can use the following to find all checked checkboxes, and retrieve their value (or whichever attribute you choose to use). 修复此问题后,您可以使用以下内容查找所有选中的复选框,并检索其值(或选择使用的任何属性)。

$("input:checked").map(function () { return $(this).val(); });
$("#savedoctorschedule").click(function(e){
    e.preventDefault();
    var sList = "";
    $('input[name="checkMr[]"]').each(function () {
        if(this.checked){
            sList += "(" + $(this).attr('id') + ")";
        }
    });
    alert(sList);
    }
);​

Please find the FIDDLE 请找到FIDDLE

Id's cannot start with numbers. ID不能以数字开头。 Once you've updated the id's use attr('value') or .val() to retrieve the value associated with the input. 更新ID后,请使用attr('value')或.val()检索与输入关联的值。

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

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