이것은 정말 간단해야하지만 작동하도록 할 수 없으며 Javascript를 사용하는 절대적인 초보자입니다. 여기에서 다양한 실을 살펴 보았습니다. 저는 간단한 링크에서 django 템플릿으로 작업하고 있습니다. 'Trash (14) Projects?'와 같은 onclick 확인 메시지를 추가하고 싶습니다. 확실합니까?' 여기서 14는 양식에 제출할 준비가 된 체크 박스의 수입니다. 지금은 가지고 있습니다.
<button type="submit" class="actionbutton" name="delete" onclick="return confirm('Add projects to trash - Are you sure?')" >Remove / Add To Trash</button>
물론 삭제할 프로젝트 수가 없습니다. 체크 박스의 클래스를 계산하기 위해 헤드에 스크립트를 추가 할 수 있지만 확인 대화 상자 자체에서 자바 스크립트를 어떻게 실행할 수 있는지 알고 싶습니다. 예를 들면 ...
<button type="submit" class="actionbutton" name="delete" onclick="return confirm('Add ($(":checkbox:checked").length) to trash - Are you sure?')" >Remove / Add To Trash</button>
작동하지 않는 이유는 ": checkbox : checked"가 onclick ""내부에서 이스케이프되기 때문일 수 있습니다.
누구나 이런 일을하는 '보통적인'방법이 무엇인지 올바른 방향으로 나에게 알려줍니다.
따옴표를 현명하게 사용 +
하고 유효한 문자열을 만드는 데 사용하십시오.
<button type="submit" class="actionbutton" name="delete" onclick="return confirm('Add (' + $(':checkbox:checked').length + ') to trash - Are you sure?')" >Remove / Add To Trash</button>
눈에 거슬리지 않는 이벤트 핸들러를 사용하는 것이 좋습니다. 고유 식별자를 추가 submitbutton
하고 CSS 클래스로 추가 했습니다.
<button type="submit" class="actionbutton submitbutton" name="delete" >Remove / Add To Trash</button>
Native Vanilla JS를 사용하여 이벤트 바인딩
document.querySelector('.submitbutton').addEventListener("click", function (event) {
var msg = 'Add (' + document.querySelectorAll('[type=checkbox]:checked').length + ') to trash - Are you sure?';
if (!confirm(msg)) {
event.preventDefault();
}
});
jQuery
$('.submitbutton').on('click', function(){
return confirm('Add (' + $(':checkbox:checked').length + ') to trash - Are you sure?')
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다