缺少jqueryajax成功设置
我有一个简单的复选框,可以通过查询数据库来打开或关闭它。用户可以更改此值,我通过缺少jqueryajax成功设置,jquery,Jquery,我有一个简单的复选框,可以通过查询数据库来打开或关闭它。用户可以更改此值,我通过ajax发送请求。url很奇怪,因为它是Joomla组件(对于这个问题不重要,但只是以防万一) $(函数(){ $(“#iButton”).iButton(); $('#iButton')。更改(函数(){ var checkedLength=$(此+':checked')。长度; 如果(1==checkedLength){ $.ajax({ url:'index.php?option=com\u mycompone
ajax
发送请求。url很奇怪,因为它是Joomla组件(对于这个问题不重要,但只是以防万一)
$(函数(){
$(“#iButton”).iButton();
$('#iButton')。更改(函数(){
var checkedLength=$(此+':checked')。长度;
如果(1==checkedLength){
$.ajax({
url:'index.php?option=com\u mycomponent&task=globaldetection&id\u hash=&global\u monitoring='+checkedLength+'&format=raw'
});
}否则{
$.ajax({
url:'index.php?option=com\u mycomponent&task=globaldetection&id\u hash=&global\u monitoring='+checkedLength+'&format=raw'
});
}
});
});
您根本不需要if/else结构,因为在两个分支中,您都有一行相同的代码,因此如果您仅将该行代码放在自己的代码中,它就可以工作(它仍将使用checkedLength
的值)
但是,$(this+':checked')。长度没有意义-选择器将字符串':checked'
连接到对象this
的末尾。我现在不明白这怎么行得通。将结果作为“长度”获取的等效方法是$(this).filter(':checked').length
,这有点笨重。或者,您可以使用if($(this).is(':checked'))
将其作为布尔值进行测试,除非这比this.checked
执行相同任务时需要的复杂得多
考虑到您可以使用this.checked直接获得checked状态,我认为以下方法更简单(而且肯定更有效):
$('#iButton')。更改(函数(){
$.ajax({
url:'index.php?option=com\u mycomponent&task=globaldetection&id\u hash=&global\u monitoring='
+(this.checked?1:0)+'&format=raw'
});
});
(我假设您最初使用的.length
只能是0
或1
,因为事件位于您通过id选择的元素上。)
“我只传递url,没有其他设置。我一直使用“成功”设置,但在这种情况下,我不需要执行其他响应。我知道这是可选的,但此时我是否应该执行其他操作?”
不需要。如果您不需要在成功时执行任何操作,则不要指定成功回调-但最好指定错误回调,以防调用因某种原因无法工作。两个ajax请求都是相同的。那么为什么有2个呢?它们不相同,checkedLength
是1或0。因此,我可以在url周围放置一个if语句,而不是复制整个ajax
调用。虽然我不确定这是否可能。无论哪种方式,这都是问题的重点#1,一定有更好的方式。@zerkms有一个观点,删除if。变量checkedLength
的使用使得if冗余。另外,查询字符串中的PHP是否执行了您希望它执行的操作?@GerardSexton好的,明白了。是的,PHP查询字符串起到了作用。“你为什么这么问?”汤姆:很好。只是检查一下。解释得很清楚,谢谢。但由于某种原因,它不起作用。如果我使用var checkLength=$(this).filter(':checked').length
和它所做的单个ajax调用。有什么想法吗?关于它为什么不起作用,我唯一的想法是,如果您似乎正在使用的iButton()
插件导致直接检查DOM属性出现问题。使用(this.checked?1:0)
在这里可以正常工作:,因此用它代替$(this.filter(':checked').length应该没有问题。无论如何,如果您在原始代码中不做任何更改,那么删除if/else并单独使用$.ajax()
行仍然是一个很大的改进。
$(function() {
$("#iButton").iButton();
$('#iButton').change(function() {
var checkedLength = $(this + ':checked').length;
if(1 == checkedLength){
$.ajax({
url: 'index.php?option=com_mycomponent&task=globaldetection&id_hash=<?php echo $id_hash; ?>&global_monitoring='+checkedLength+'&format=raw'
});
} else {
$.ajax({
url: 'index.php?option=com_mycomponent&task=globaldetection&id_hash=<?php echo $id_hash; ?>&global_monitoring='+checkedLength+'&format=raw'
});
}
});
});
<?php
//highlight whether the checkbox is on or off
if ($userstatus == "ENABLED") //get this value from server
{
//turned on
$global_monitoring = "checked=\"checked\"";
}
else
{
$global_monitoring = "";
}
?>
<div class="dropshadow">
<p id="iButtontext">
<input type="checkbox" id="iButton" <?php echo $global_monitoring; ?> name="iButton_checkbox" />
</p>
</div>
$('#iButton').change(function() {
$.ajax({
url: 'index.php?option=com_mycomponent&task=globaldetection&id_hash=<?php echo $id_hash; ?>&global_monitoring='
+ (this.checked ? 1 : 0) + '&format=raw'
});
});