H_MZ 2014-05-02 04:30 采纳率: 0%
浏览 40

Ajax Alert触发两次

The alert keeps firing twice, but not sure why. Any suggestions? .onoffswitch is a checkbox.

<script type="text/javascript">
    $(document).ready(function(){
        $('.onoffswitch').click(function(){
            var hiddenValueID = $(this).children(':hidden').val();
            if ($(this).children(':checked').length == 0)
            {
                var valueData = '0';
            }
            else
            {
                var valueData = '1';
            }

            $.ajax({
                type: "POST",
                url: "ajax.php",
                data: {value: valueData, ID: hiddenValueID} ,
                success: function(html){
                    $("#display").html(html).show();
                }
            });

        });
    });
</script>

ajax.php...

<?php
include('connect.php');

if(isset($_POST['value'])){

    $value=$_POST['value'];
    $id=$_POST['ID'];

    $sql = "UPDATE campaigns SET Status=? WHERE ID=?";
    $q = $db->prepare($sql);
    if($q->execute(array($value,$id))) {
        echo '<script>alert("Campaign Updated");</script>';
        die();
    } else{
        echo '<script>alert("Change Failed - Try Again");</script>';
        die();
    }
}
?>
  • 写回答

3条回答 默认 最新

  • weixin_33695450 2014-05-02 04:45
    关注

    Is .onoffswitch a checkbox? If so, I believe that both the click and the uncheck/check are bound to the click event.

    Try using .mouseup() instead of .click() and see if you have the same problem.

    评论

报告相同问题?