简体   繁体   English

为什么这个if / else在我的jquery中不起作用?

[英]Why does this if/else not work in jquery for me?

I have the following that fires off when a checkbox is changed. 我有以下内容在更改复选框时触发。

$(document).ready(function() {
    $("#reviewed").change(function(){
        if ($('#reviewed:checked').val() !== null) {
            $.ajax({
                url: "cabinet_reviewed.php?reviewed=yes",
                cache: false,
                success: function(html){
                    $("#reviewDate").replaceWith(html);
                }
            });
         } else {
             $.ajax({
                url: "cabinet_reviewed.php?reviewed=no",
                cache: false,
                success: function(html){
                    $("#reviewDate").replaceWith(html);
                }
            });
        }
});
})

This only works once. 这只适用一次。 I'm looking to see when the check box is changed and what the value of it is once changed. 我希望看到复选框何时更改以及更改后的值是什么。

UPDATE: I've change the code around to the following (based on everyone's comments) 更新:我已将代码更改为以下内容(基于每个人的评论)

$(document).ready(function() {
    $("#reviewed").click(
        function() { 
            var rURL = 'cabinet_reviewed.php?reviewed=';
                if ($("#reviewed").is(":checked"))
                   rURL = rURL + "yes";
                else
                   rURL = rURL + "no";
                alert (rURL);
        $.ajax({
            url: rURL,
            cache: false,
            success: function(html){
                $("#reviewDate").replaceWith(html);
            }
        });

    });

}) })

The file cabinet_reviewed.php simply echos the value of $_GET['reviewed'] With this updated code the alert shows the correct URL but the second click does not run the .ajax. 文件cabinet_reviewed.php只是回显$ _GET ['review']的值。使用此更新的代码,警报显示正确的URL,但第二次单击不运行.ajax。 Do I need to do something so the .ajax is run again? 我是否需要做一些事情以便再次运行.ajax?

There are apparently some bugs with the change() event for checkboxes in IE. 对于IE中的复选框,显然存在一些bug change()事件的错误。 Try using the click() event instead and see if it works better. 尝试使用click()事件,看看它是否更好。

Try with != instead of !== 试试!=而不是!==

And also this as an alternative: 而这也是另一种选择:

$('#reviewed').is(':checked')

The below code works consistently in FF 3.5 and IE8: 以下代码在FF 3.5和IE8中一致地工作:

$("#reviewed").click(
    function() { 
        if ($("#reviewed").is(":checked"))
            alert('checked'); 
        else
            alert('not checked');
    }
);              

After your update: 更新后:

This code... 这段代码......

success: function(html){
  $("#reviewDate").replaceWith(html);
}

... is replacing the element with ID=reviewDate in the DOM with the HTML that is returned from the Ajax call, so it is no longer present the second time the Ajax call is made. ...正在使用从Ajax调用返回的HTML替换DOM中ID=reviewDate的元素,因此在第二次进行Ajax调用时它不再存在。

Will something simpler like this work for you? 这个更简单的东西会为你工作吗?

success: function(html){
  $("#reviewDate").html(html);
}

You normally want to use click event to track changes in checkboxes/radiobuttons. 您通常希望使用click事件来跟踪复选框/单选按钮中的更改。 The change event is only fired if the new value differs from the old value. 仅当新值与旧值不同时才会触发change事件。 In checkboxes/radiobuttons there's no means of an initial value, only the checked attribute which is often not predefinied, hence the need to click twice before the change event is fired. 在checkboxes / radiobuttons中,没有初始值的方法,只有通常不预定义的checked属性,因此需要在触发change事件之前单击两次。

In checkboxes/radiobuttons you also don't want to check the value by val() , it's always the same. 在复选框/单选按钮中,您也不希望通过val()检查值,它始终是相同的。 You rather want to check the checked state using this.checked . 您更愿意使用this.checked检查已检查状态。

Thus, the following should work: 因此,以下应该工作:

$(document).ready(function() {
    $("#reviewed").click(function() {
        if (this.checked) {
            // ...
        } else {
            // ...
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM