不使用表单而不进行提交但页面刷新 - 为什么?

时间:2017-04-01 14:24:34

标签: javascript jquery

下面的代码不使用表单,也不进行提交,然后点击按钮重新加载页面。根据我读过的所有内容,不应该重新加载。我不明白为什么。有人可以帮忙吗?

我还尝试将事件功能添加到按钮(请参阅更新的源代码)并更新了下面的源代码。它现在确实有用了

<SCRIPT>
    $( document ).ready(function() {

    $('input[type=button]').hover(function() {
        $(this).toggleClass('ui-state-hover');
    });

    // get the php vars into local vars
    var req_status = '<?=$req_status?>';
    var member_id  = '<?=$mbr_id?>';
    var group_id   = '<?=$grp_id?>';

    if (req_status == 'member-is-grp-ldr') {
        $("#ok").prop('value', 'Unsubscribe'); 
        $('#ok').prop('disabled',true);
        $('#ok').addClass('ui-state-disabled');
    } else if (req_status == 'member-not-in-group') {
        $("#ok").prop('value', 'Resubscribe'); 
        $('#ok').prop('disabled',false);
    } else if (req_status == 'member-removed') {
        $("#ok").prop('value', 'Resubscribe'); 
        $('#ok').prop('disabled',false);
        $("#close").prop('value', 'Close');
    } else {
        $("#ok").prop('value', 'Unsubscribe'); 
        $('#ok').prop("disabled",false);
    }

   $('#ok').on('click', function(e) { 
        e.preventDefault();
        if ($('#ok').val() == "Unsubscribe") {
            $.post('./ajax_group_unsubscribe.php', {function:'unsubscribe', member_id:member_id, group_id:group_id}, function(status) {
                if (status = 'true') {
                    $("#ok").prop('value', 'Resubscribe'); 
                    $('#ok').prop('disabled',false);
                    $('#ok').removeClass('ui-state-disabled');
                    $("#close").prop('value', 'Close'); 
                    document.getElementById('remark').innerHTML = "You have been removed from the group:";  
                };
            });
        } else if ($('#ok').val() == "Resubscribe") {
            $.post('./ajax_group_unsubscribe.php', {function:'subscribe', member_id:member_id, group_id:group_id }, function(status) {                  
                if (status = 'true') {
                    $("#ok").prop('value', 'Unsubscribe'); 
                    $('#ok').prop('disabled',false);
                    $('#ok').removeClass('ui-state-disabled');
                    $("#close").prop('value', 'Close');
                    document.getElementById('remark').innerHTML = "You have been added to the group:";      
                };
            });
        }
    });
});

</SCRIPT>
</HEAD>

<BODY>
    <div id="page-wrap">
        <div id="header_wrapper">
            Group Unsubscribe Request
        </div>
        <div id="groups_bg">
            <?php
                if ($req_status == "member-in-group") {
                    echo "<p id='remark'>Confirm you no longer want to receive messages for the group:</p>";
                    echo "<p><em>" . $grp_name . "</em></p>";
                } else if ($req_status == "member-not-in-group") {
                    echo "<p id='remark'>You are not currently in the group:</p>";
                    echo "<p><em>" . $grp_name . "</em></p>";
                } else if ($req_status == "member-removed") {
                    echo "<p id='remark'>You will no longer receive messages for the group:</p>";
                    echo "<p><em>" . $grp_name . "</em></p>";   
                } else {
                    echo "<p id='remark'>You are the group leader and may not unsubscribe from the group:</p>";
                    echo "<p><em>" . $grp_name . "</em></p>";
                }
            ?>
            <br>
            <div style="text-align:center;">
            <input type="button" id="ok" name="ok" class='ui-button ui-widget ui-state-default ui-corner-all' value="Unsubscribe">
            <input type="button" id="close"  name="close"  class='ui-button ui-widget ui-state-default ui-corner-all' value="Cancel" onclick="window.open('', '_self', ''); window.close();">
            </div>
        </div>
    </div>
</BODY>
</HTML>

这是ajax代码:

/* get a database object */
$db = new DatabaseA();

/* get the member id and group id */
$function  = $_POST['function'];
$member_id = $_POST['member_id'];
$group_id  = $_POST['group_id'];

/* initialize the return status - booleans not returned */
$ret_status = 'false';

/* if the member id and group id were provided then */
if ( ! empty($member_id) && ! empty($group_id) ) {

    /* get organization id for the group */
    $group_info = get_group_info($db, $group_id, "org_id, group_name");
    $org_id = $group_info['org_id'];
    $grp_name = $group_info['group_name'];

    /* get the group leader */
    $grp_ldr_id = get_group_leader($db, $group_id);

    /* if the member is not the group leader then */
    if ($member_id <> $grp_ldr_id) {

        /* if the member is unsubscribing then */
        if ( $function = "Unsubscribe" && ! empty($org_id) ) {

            /* delete the member from the group */
            remove_member_from_group($db, $member_id, $group_id, $org_id);

            /* notify the group leader */
            send_member_email($db, $grp_ldr_id, "Group Member Update", "Member " . $mbrfirst_name . " " . $mbrlast_name . " has unsubscribed from your group: " . $grp_name);

            /* indicate the member was unsubscribed */
            $ret_status = 'true';

        /* else if the member is resubscribing then */
        } else if ($function = "Resubscribe" && ! empty($org_id) ) {

            /* add the member to the group */
            add_member_to_group($db, $member_id, $group_id, $org_id);

            /* notify the group leader */
            send_member_email($db, $grp_ldr_id, "Group Member Update", "Member " . $mbrfirst_name . " " . $mbrlast_name . " has resubscribed to your group: " . $grp_name);

            /* indicate the member was subscribed */
            $ret_status = 'true';
        }
    }
}

/* return to the caller */
return ($ret_status);

2 个答案:

答案 0 :(得分:0)

您在=语句中使用单else,并且在内部if语句中缺少结束花括号。

$('#ok').on('click', function() { 
    if ($('#ok').value == "Unsubscribe") {
        $.post('./ajax_group_unsubscribe.php', {function:'unsubscribe', member_id:member_id, group_id:group_id}, function(status) {
            if (status = 'true') {
                $("#ok").prop('value', 'Resubscribe'); 
                $('#ok').prop('disabled',false);
                $('#ok').removeClass('ui-state-disabled');
                $("#close").prop('value', 'Close'); 
                document.getElementById('remark').innerHTML = "You have been removed from the group:";
            }             
        });
    } else if ($('#ok').value == "Resubscribe") {
        $.post('./ajax_group_unsubscribe.php', {function:'subscribe', member_id:member_id, group_id:group_id }, function(status) {                  
            if (status = 'true') {
                $("#ok").prop('value', 'Unsubscribe'); 
                $('#ok').prop('disabled',false);
                $('#ok').removeClass('ui-state-disabled');
                $("#close").prop('value', 'Close');
                document.getElementById('remark').innerHTML = "You have been added to the group:";
            }    
        });
    }
});

答案 1 :(得分:-1)

而不是<input></input>您应该使用<button type="button"></button>。 单击输入将被解释为提交。