我正在尝试删除帖子。有一个模式,询问您是否要删除。下面是我的脚本。我遇到的问题是我无法将我的变量postID用于我调用ajax的函数中。我知道这是我的postID变量是局部变量的问题。如何使用该变量以便在以后的函数中调用它。我想把它变成全球性的,但是当我试图这样做时它没有用。 (其中'+'的出现是由于这个脚本被php回应)
<script>
$(document).ready(function(){
bindFunc()
$('.creatorPostOptions').click(function(){
var postID = $(this).val();
$('.postOptionsFull').show();
$('.postOptionsFullContent').append('<button id='+'editPostButton'+'>Edit</button><button id='+'deletePostButton'+' value='+''+'>Delete</button><button id='+'cancelPostButton'+'>Cancel</button>');
bindFunc();
});
function bindFunc() {
$('#cancelPostButton').click(function(){
$('.postOptionsFull').hide();
$('.postOptionsFullContent').children('button').remove();
});
$('#deletePostButton').click(function(){
$('.postOptionsFullContent').hide();
$('.deletePostConfirmation').show();
$('.deletePostButtons').append('<button id='+'cancelDeletePost'+'>Cancel</button><button id='+'deletePost'+'>Delete</button>');
deletePostFunction();
});
}
function deletePostFunction() {
$('#cancelDeletePost').click(function(){
$('.postOptionsFullContent').show();
$('.deletePostConfirmation').hide();
$('.deletePostButtons').children('button').remove();
});
$('#deletePost').click(function(){
var postID = postID;
$.ajax({
url: deletePost.php,
method: 'POST',
data:{postID:postID},
success:function(data){
window.location.reload();
}
});
});
}
$('.noncreatorPostOptions').click(function(){
$('.postOptionsPartial').show();
$('.postOptionsPartialContent').append('<button id='+'reportPostButton'+'>Report</button><button id='+'cancelReportButton'+'>Cancel</button>');
bindFuncTwo();
});
function bindFuncTwo() {
$('#cancelReportButton').click(function(){
$('.postOptionsPartial').hide();
$('.postOptionsPartialContent').children('button').remove();
});
}
});
</script>
答案 0 :(得分:0)
尝试在函数外定义var?
$(document).ready(function(){
var postID;
bindFunc()
$('.creatorPostOptions').click(function(){
postID = $(this).val();
$('.postOptionsFull').show();
$('.postOptionsFullContent').append('<button id='+'editPostButton'+'>Edit</button><button id='+'deletePostButton'+' value='+''+'>Delete</button><button id='+'cancelPostButton'+'>Cancel</button>');
bindFunc();
});
function bindFunc() {
$('#cancelPostButton').click(function(){
$('.postOptionsFull').hide();
$('.postOptionsFullContent').children('button').remove();
});
$('#deletePostButton').click(function(){
$('.postOptionsFullContent').hide();
$('.deletePostConfirmation').show();
$('.deletePostButtons').append('<button id='+'cancelDeletePost'+'>Cancel</button><button id='+'deletePost'+'>Delete</button>');
deletePostFunction();
});
}
function deletePostFunction() {
$('#cancelDeletePost').click(function(){
$('.postOptionsFullContent').show();
$('.deletePostConfirmation').hide();
$('.deletePostButtons').children('button').remove();
});
$('#deletePost').click(function(){
postID = postID;
$.ajax({
url: deletePost.php,
method: 'POST',
data:{postID:postID},
success:function(data){
window.location.reload();
}
});
});
}
$('.noncreatorPostOptions').click(function(){
$('.postOptionsPartial').show();
$('.postOptionsPartialContent').append('<button id='+'reportPostButton'+'>Report</button><button id='+'cancelReportButton'+'>Cancel</button>');
bindFuncTwo();
});
function bindFuncTwo() {
$('#cancelReportButton').click(function(){
$('.postOptionsPartial').hide();
$('.postOptionsPartialContent').children('button').remove();
});
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
答案 1 :(得分:0)
JavaScript是一种函数范围的语言,而不是像PHP这样的块范围语言。初始化&#39; .creatorPostOptions&#39;的单击处理函数内的变量postID时,在该单击处理程序之外无法访问它。
您可以根据@ GavinLuo的建议,在.creatorPostOptions的单击句柄之外定义变量(并赋值)。但是,我不确定我理解为什么所述帖子的ID将被设置为$(this).val()。不应该通过HTML ID属性在DOM中表示其ID?