繁体   English   中英

通过ajax运行php页面时,检查在javascript中单击了哪个按钮

[英]check which button has been clicked in javascript when running a php page via ajax

我在博客文章上有一个显示两个提交按钮的表格。

一种是保存,另一种是删除。 例如:

<form class="updatepost">
<input type="submit" name="saveupdatebutton" class="saveupdatebutton" value="Save">
<input type="submit" name="deleteupdatebutton" class="deleteupdatebutton" value="Delete">
</form>

目前,我有这个js:

// JavaScript - Edit Post

$(document).ready(function(){
$(".updatepost").submit(function(){
    var $targetForm = $(this);

    $targetForm.find(".error").remove();
    $targetForm.find(".success").remove();

    // If there is anything wrong with 
    // validation we set the check to false
    var check = true;

    // Get the value of the blog update post
    var $ckEditor = $targetForm.find('.ckeditor'),
        blogpost = $ckEditor.val();


            // Validation
    if (blogpost === '') {
        check = false;
       $(this).css('border', 'solid 1px red');
    } else {
        $(this).css('border', 'none');
    }

      // ... goes after Validation
    if (check) {
    $.ajax({
    type: "POST",
    url: "process/updatepost.php",
    data: $targetForm.serialize(),
    dataType: "json",
    success: function(response){

    if (response.databaseSuccess)
       $targetForm.find('.editor').toggle(),
       <!-- Check for .buildtext id etc to prevent it showing any other posts when updating it -->
       $targetForm.find('#'+response.postid+'').load(''+response.pageurl+' #'+response.postid+''),
       $targetForm.find('#'+response.postid+'').toggle(), 
       $targetForm.find('.edity').toggle(),
       $targetForm.find('.saveupdatebutton').toggle(),
       $targetForm.find('.deleteupdatebutton').toggle();
    else
       $ckEditor.after('<div class="error">Something went wrong!</div>');

}
        });
    }
    return false;
});
});

提交表单时,这显然会运行代码。 但是我想根据单击什么按钮来运行不同的js ajax调用。

我已经试过了:

// JavaScript - Edit Post

$(document).ready(function(){
$(".saveupdatebutton").click(function(){
     DO STUFF ETC...
});
});

但这不起作用。 任何帮助我如何确定单击哪个按钮,然后执行不同的js代码的任何帮助。

可以尝试类似:

$(".saveupdatebutton").click(function(){
     $(this).addClass('activeBtn');
});

$(".updatepost").submit(function(){
   var isSave= $(this).find('.saveupdatebutton').is('.activeBtn');

   if( isSave){
       /* save code*/
   }else{
        /* delete code*/
    }

})

暂无
暂无

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

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