获取复选框的ID?- jQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3363957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get id of a checkbox? - jQuery
提问by DonDraper
Hey, I am wondering how to get the id of a checkbox when it is checked.
嘿,我想知道如何在选中时获取复选框的 id。
This is what my HTML would probably look like at first:
这是我的 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 -->
I'm assuming the jQuery would look something like this:
我假设 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;
});
});
I'm trying to get the checked item id and then post that id in a mysql query to grab the results of the id from the database.
我正在尝试获取已检查的项目 id,然后将该 id 发布到 mysql 查询中以从数据库中获取该 id 的结果。
Thanks for any help on this.
感谢您对此的任何帮助。
回答by Nick Craver
You probably want the change
event here, like this:
你可能想要change
这里的事件,像这样:
$(function() {
$(":checkbox").change(function(){
$.post("index.php", { id: this.id, checked: this.checked });
});
});
This posts whenever a checkbox changes, and lets the PHP side know the ID and whether it's checked or not.
每当复选框更改时,它就会发布,并让 PHP 端知道 ID 以及它是否被选中。
回答by TheCloudlessSky
The jQuery you provided works fine? You should probably remove the return false
to showthat the checkbox has been checked.
您提供的 jQuery 工作正常吗?您可能应该删除return false
以显示该复选框已被选中。
EDIT: $.post()is for an async request (AJAX technique) so there will be no page refresh.
编辑:$.post()用于异步请求(AJAX 技术),因此不会刷新页面。
EDIT 2: You may also want to see if the checkbox is checked(not just that it has been clicked):
编辑 2:您可能还想查看复选框是否被选中(不仅仅是它已被点击):
$(document).ready(function() {
$(":checkbox").click(function(){
var id = $(this).attr('id');
var isChecked = $(this).attr('checked'));
$.post("index.php", { id: id, isChecked: isChecked });
});
});
回答by Jonathan Burgos Gio
You can use the :checkbox
selector and then check the id
and checked properties of the DOM element like so:
您可以使用:checkbox
选择器,然后id
像这样检查和检查 DOM 元素的属性:
var id = $(this).find(":checkbox")[0].id;
var isChecked = $(this).find(":checkbox")[0].checked;
var id = $(this).find(":checkbox")[0].id;
var isChecked = $(this).find(":checkbox")[0].checked;
回答by filster
You can better use jQuery get instead of post. It allows you to handle the output of your request: http://api.jquery.com/jQuery.get/
您可以更好地使用 jQuery get 而不是 post。它允许您处理请求的输出:http: //api.jquery.com/jQuery.get/
Example:
例子:
$(document).ready(function() {
$(':checkbox').click(function(){
var id = $(this).attr('id');
var isChecked = $(this).attr('checked'));
$.get(
'getcheckbox.php',
{ id: id, isChecked: isChecked },
function(data) {
//return value in getcheckbox.php and use it in on complete function
var result = data;
}
);
});
});
回答by Simon H
Why do I only need this for the name of a checkbox, but $(this) in order to access the value
为什么我只需要这个作为复选框的名称,但 $(this) 才能访问该值
$('input:checkbox').change( function() {
console.log("here i am: " + this.name + " value = " + $(this).val() );
}
);