jQuery Ajax 复选框状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3117673/
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
jQuery Ajax checkbox state
提问by oshirowanen
I have checkboxes on my page for which I would like to send their state back to the database via ajax. I know how to use jquery with ajax, but I don't know how to get the checked state, both checked and unchecked along with the id of the checkbox so I can send it back to the server.
我的页面上有复选框,我想通过 ajax 将它们的状态发送回数据库。我知道如何将 jquery 与 ajax 一起使用,但我不知道如何获得选中状态、选中状态和未选中状态以及复选框的 ID,以便我可以将其发送回服务器。
Any ideas?
有任何想法吗?
回答by Ain Tohvri
if ($("#yourCheckboxID").is(":checked")) {
// checkbox is checked
} else {
// checkbox is not checked
}
will do the job.
会做的工作。
回答by oshirowanen
Something like this:
像这样的东西:
<script type="text/javascript">
$(document).ready(function(){
$("input:checkbox").change(function() {
if($(this).is(":checked")) {
$.ajax({
url: 'on_off.aspx',
type: 'POST',
data: { strID:$(this).attr("id"), strState:"1" }
});
} else {
$.ajax({
url: 'on_off.aspx',
type: 'POST',
data: { strID:$(this).attr("id"), strState:"0" }
});
}
});
});
</script>
回答by chridam
Combining your solution and the accepted answer by Ain:
结合您的解决方案和 Ain 接受的答案:
<script type="text/javascript">
$(document).ready(function(){
var isChecked = $("input:checkbox").is(":checked") ? 1:0;
$.ajax({
url: 'on_off.aspx',
type: 'POST',
data: { strID:$("input:checkbox").attr("id"), strState:isChecked }
});
});
</script>
回答by stuart
Adding back in the change event to the merged solution. Want this to fire every time checkbox is changed.
将更改事件添加回合并的解决方案。希望每次更改复选框时触发。
<script type="text/javascript">
$(document).ready(function(){
$("input:checkbox").change(function() {
var isChecked = $("input:checkbox").is(":checked") ? 1:0;
$.ajax({
url: 'on_off.aspx',
type: 'POST',
data: { strID:$("input:checkbox").attr("id"), strState:isChecked }
});
});
});
</script>
回答by Pointy
Well it's easy enough to find the checkboxes:
很容易找到复选框:
$(':checkbox').whatever()
The trick is that in HTML checkboxes only have a value that's meaningful when checked. When they're not checked, what do you tell the server?
诀窍是,在HTML复选框只有一个值时,这是有意义的检查。当他们没有被检查时,你告诉服务器什么?
Well if you've got a convention to work with (perhaps always sending "true" when checked and "false" when not checked), the next thing you have to decide is how to get them to your server. You can use the jQuery param
function to turn the list into a parameter string:
好吧,如果您有一个约定可以使用(可能总是在选中时发送“true”而在未选中时发送“false”),接下来您必须决定如何将它们发送到您的服务器。您可以使用 jQueryparam
函数将列表转换为参数字符串:
var params = $.param($(':checkbox').map(function() {
return { name: this.id, value: !!this.checked };
}));
That gives you a string ready to be tacked onto a URL, or sent as data via $.ajax
.
这为您提供了一个准备好添加到 URL 上的字符串,或通过$.ajax
.