ajax在特定时间后加载内容

时间:2017-01-20 15:11:45

标签: javascript jquery

我试图从数据库中获取数据而不重新加载。所以我做了这个

Code In PasteBin

执行此操作后,这是点击

的ajax部分

    
    function ed_action(p_authority_id, ed_value) {
        $.ajax({
            method: "POST",
            url: "inc/ed_action.php",
            data: 'p_authority_id='+ encodeURIComponent(p_authority_id) + '&ed_value='+ encodeURIComponent(ed_value),
            success: function(data) {
                if(data){
                    $("#show").html(data);
                }
            }
        });
    }


这是ed_action.php文件

$func = new functions();

if($_SERVER["REQUEST_METHOD"] == "POST"){

    $ed_value = $_POST['ed_value'];
    $p_authority_id = $_POST['p_authority_id'];

    $user_data = array(
        'ed_action'   => $ed_value
    );

    $where_cond = array(
        'where' => array('p_authority_id' => $p_authority_id),
        'cross_check' => 'and',
        'return_type' => 'single'
    );
    $table_name = 'p_authrity_user';

    $update = $func->update($table_name, $user_data, $where_cond);
    $ed_action_data = $func->select($table_name, $where_cond);
}

我通过点击成功检索了数据。但是现在我希望当我点击启用按钮时它会显示禁用按钮而不重新加载,当点击禁用时,它应该显示启用按钮。那我该怎么办?

请你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

正如我在评论中提到的:在你的ajax请求的成功函数中,你可以切换两个按钮的可见性。

function ed_action(p_authority_id, ed_value) {
    $.ajax({
        method: "POST",
        url: "inc/ed_action.php",
        data: 'p_authority_id='+ encodeURIComponent(p_authority_id) + '&ed_value='+ encodeURIComponent(ed_value),
        success: function(data) {
            if(data){
                $("#show").html(data);
                toggleButtons();
            }
        }
    });
}

function toggleButtons(){
    var enableBtn = $('button.ed_action.ed_action_enable'); // add class ed_action_enable to button so you can get it here
    var disableBtn = $('button.ed_action.ed_action_disable'); // add class ed_action_disable to button so you can get it here

    enableBtn.toggle(); // toggles visibility of element
    disableBtn.toggle(); // toggles visibility of element

    /* shorter code for this:
    $('button.ed_action.ed_action_enable').toggle;
    $('button.ed_action.ed_action_disable').toggle;
    */
}

为此,你需要

1:将两个按钮输出到html(禁用btn hidden)

2:在按钮上再添加一个类,这样您就可以使用jquery轻松获取它们