嘿,我想知道如何检查复选框的ID.
这是我的HTML最初看起来可能是这样的:
<div class="check_filter">
<div id="filter">
<input type="checkbox" id="check1" /><label for="check1">Marketing</label>
<input type="checkbox" id="check2" /><label for="check2">Automotive</label>
<input type="checkbox" id="check3" /><label for="check3">Sports</label>
</div>
</div><!-- End check_filter -->
Run Code Online (Sandbox Code Playgroud)
我假设jQuery看起来像这样:
$(document).ready(function() {
$(":checkbox").click(function(){
var id = $(this).attr('id');
$.post("index.php", { id: id });
//return false to ensure the page doesn't refresh
return false;
});
});
Run Code Online (Sandbox Code Playgroud)
我正在尝试获取已检查的项目ID,然后在mysql查询中发布该ID以从数据库中获取id的结果.
感谢您的帮助.
Nic*_*ver 17
你可能想要change这里的活动,像这样:
$(function() {
$(":checkbox").change(function(){
$.post("index.php", { id: this.id, checked: this.checked });
});
});
Run Code Online (Sandbox Code Playgroud)
只要复选框发生变化,这就会发布,并让PHP方知道ID以及是否检查过它.
你提供的jQuery工作正常吗?您应该删除return false以显示已选中复选框.
编辑:$ .post()用于异步请求(AJAX技术),因此不会刷新页面.
编辑2:您可能还想查看复选框是否已选中(不仅仅是已单击):
$(document).ready(function() {
$(":checkbox").click(function(){
var id = $(this).attr('id');
var isChecked = $(this).attr('checked'));
$.post("index.php", { id: id, isChecked: isChecked });
});
});
Run Code Online (Sandbox Code Playgroud)