我有下面的代码。
<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“的复选框,而不是仅仅侦听第一个复选框?
发布于 2014-01-15 00:03:41
应该向输入中添加一个类,并使用类选择器(如$(".myClass") )。
发布于 2014-01-15 00:04:43
id属性是唯一的。
来自JQuery文档
每个id值只能在文档中使用一次。如果已为多个元素分配了相同的ID,则使用该ID的查询只会选择DOM中的第一个匹配元素。
这就是为什么它只适用于你的第一个。
您应该使用类选择器。
https://stackoverflow.com/questions/21126626
复制相似问题