我在一篇博客文章中有一个表单,其中显示了两个提交按钮。
一个是保存,另一个是删除。例如:
<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*/
}
})