简体   繁体   English

jQuery:在选中或取消选中复选框时无法显示加载相应页面

[英]Jquery: unable to show load the respective pages while checking or unchecking the checkbox

I have a check box. 我有一个复选框。 I want to call two different php pages based on the checkbox checked and uncheck status. 我想根据已checked和未uncheck状态来调用两个不同的php页面。

But the below code is only calling the checked php pages. 但是下面的代码仅调用选中的php页面。 What could be the reason? 可能是什么原因?

     <script type="text/javascript">
     $(function()
     {
     $('input[id^="test"]').on('click',function()
       {
         if ($('input[id^="test"]').checked)//when users click to on checkbox to check
         {

           $.ajax({
               url: "updatestatus.php?id="+$(this).val()+"&status=1",
               success: function(data){ 
                alert('Successfully updated...');
               }
             });
     } 
     else//when users uncheck the checkbox
       {    $.ajax({
               url: "updatestatus.php?id="+$(this).val()+"&status=2",
               success: function(data){
                 alert("uncheckde...");
               }
             });
        } 
        });
    });
         </script>

And in page 并在页面中

<?php //checkbox in a while loop

            echo " <input type='checkbox' id='test' value='".$row['id']."'/> ";
         ?>

It's your check for checked or not that isn't working. 这是您检查的支票,否则支票不起作用。 Use the .is(':checked') method instead. 请改用.is(':checked')方法。

$(document).ready(function() {
    $('input[name=cbx]').change(function() {
        if ($(this).is(':checked')) {
            alert('Checked');
        } else {
            alert('Not Checked');
        }
    });
});

As requested: 按照要求:

Try using $('input[id^="test"]').is(':checked') as the conditional. 尝试使用$('input[id^="test"]').is(':checked')作为条件。 You can also use $(this) to reference the specific checkbox that triggered the event. 您也可以使用$(this)引用触发事件的特定复选框。 So, here's your code cleaned up a bit: 所以,这是您的代码清理了一下:

<script>
    $(function() {
        $('input[id^="test"]').on('click',function(e) {
            if ($(this).is(':checked') {
                $.ajax({
                    url: "updatestatus.php?id="+$(this).val()+"&status=1",
                    success: function(data){ 
                        alert('Successfully updated...');
                    }
                });
            } 
            else {    
                $.ajax({
                    url: "updatestatus.php?id="+$(this).val()+"&status=2",
                    success: function(data){
                        alert("uncheckde...");
                    }
                });
            } 
        });
    });
</script>

One other change I would make would be to assign a class to your checkboxes, and target that instead of using the "starts with" selector (since not every browser supports it). 我要进行的另一项更改是为您的复选框分配一个类,并以该类为目标,而不是使用“开头为”选择器(因为并非每个浏览器都支持它)。

Try like this 这样尝试

$(function()
     {
     $('input[id^="test"]').on('click',function()
       {
         if ($(this).attr("checked") == "checked")//when users click to on checkbox to check
         {

           $.ajax({
               url: "updatestatus.php?id="+$(this).val()+"&status=1",
               success: function(data){ 
                alert('Successfully updated...');
               }
             });
     } 
     else//when users uncheck the checkbox
       {    $.ajax({
               url: "updatestatus.php?id="+$(this).val()+"&status=2",
               success: function(data){
                 alert("uncheckde...");
               }
             });
        } 
        });
    });

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

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