Php jquery多id选择器

Php jquery多id选择器,php,jquery,html,Php,Jquery,Html,我有以下代码 <html> <head> <title>Insert and Show Records using jQuery Ajax and PHP</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script> $(function(){ //inse

我有以下代码

<html>
<head>
<title>Insert and Show Records using jQuery Ajax and PHP</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(function(){
    //insert record
    $('#tagid').click(function(){
        //var idis = $('#tagid').val();
        var idis = document.getElementById("tagid").name;
        alert(idis);

        //syntax - $.post('filename', {data}, function(response){});
        $.post('test2.php',{action: "insert", tagidis:idis},function(res){
            $('#result').html(res);
        });        
    });

});
</script>
</head>
 <body>

<?php
$key = 1;
echo "<input type=\"checkbox\" name=\"".$key."\" id=\"tagid\"";
echo " style=\"width: 20px; height: 20px;\">";
$key = $key+1;
echo "<input type=\"checkbox\" name=\"".$key."\" id=\"tagid\" style=\"width: 20px; height: 20px;\">";
?>

<p>Result:</p>
<div id="result"></div>
</body>
</html>
当我执行它时,其中$key=1,它会发出警报并显示1。 但当我选中第二个复选框时,它不会显示警报


在jquery中,如何让它监听id为tagid的所有复选框,而不仅仅是第一个复选框?

您应该向输入中添加一个类,并使用类似$的类选择器。myClass

id属性是唯一的

从:

每个id值在文档中只能使用一次。如果为多个元素分配了相同的ID,则使用该ID的查询将只选择DOM中第一个匹配的元素

这就是为什么它只适用于第一个


您应该改用类选择器。

页面中的所有ID都必须是唯一的。在这种情况下,您可以通过单击函数:$'.myClass'.clickfunction{var idis=$this.attr'name';获得所需的内容。。。