jQuery/JavaScript가 두 번 나타납니다
-
05-07-2019 - |
문제
이상한 이유로 확인 상자가 두 번 나타납니다. 내 코드는 다음과 같습니다.
$(".DeleteComment").live("click", function(){
var CommentID = $(this).attr("rel");
var confirm
if (!confirm('Are you sure you want to permanently delete this comment?')){
return false;
}else{
$(this).html("loading").css("color", "#999");
//AJAX HERE
return false;
}
});
해결책
AJAX를 통해 동적으로 컨텐츠를로드합니까? 클릭 이벤트가 동일한 요소에 두 번 묶여 이중 확인을 초래할 수 있습니다.
다른 팁
이 시도:
$_blockDelete = false;
$(".DeleteComment").live("click", function(event){
event.preventDefault();
//event.stopPropagation(); // it is not necessary
if (!$_blockDelete)
{
$_blockDelete =true;
var rconfirm = confirm('Are you sure you want to permanently delete this comment?');
if (rconfirm)
{
$(this).html("loading").css("color", "#999");
var CommentID = $(this).attr("rel");
//AJAX HERE
//return the value "false" the variable "$_blockDelete" once again ajax response
}
}
});
우리가 이벤트에 이벤트를 묶을 때 발생합니다. Ajax를 통해 동적으로로드했습니다
예를 들어, 우리는 클릭시 동적 HTML 컨텐츠 (예 : 동적 컨텐츠)를로드하고 있습니다. edit
양식 버튼,
그리고 우리가 가지고 있다면 그 내용에서 버튼에서 이벤트를 클릭하십시오 예를 들어 delete
버튼, 클릭 할 때마다 edit
양식 버튼, 그것은 묶습니다 click
이벤트 delete
매번 버튼,
클릭 이벤트에서 확인란을 설정 한 경우 delete
그런 다음 버튼, 그것은 그 클릭 이벤트에 얽힌만큼 많은 시간을 요구할 것입니다. 클릭 한 경우 여기를 의미합니다 edit
폼 버튼을 5 번 양식 한 다음 5 번 확인을 요청합니다.
그래서 그 문제를 해결하기 위해 당신은 할 수 있습니다 unbind
바인딩 이벤트 전에 매번 이벤트 다음과 같이 동적으로로드 된 요소에 :
$(document).off('click', '.DeleteComment').on('click', '.DeleteComment', function () {
if (confirm('Are you sure you want to permanently delete this comment?')){
//Delete process
return true;
}
return false;
}
또는 이 문제를 해결하는 또 다른 방법은 메인 페이지에 스크립트를 추가하는 것입니다..
사용하지 않은 것을 제거하려고 했습니까? var confirm
?
제휴하지 않습니다 StackOverflow