2016-08-26 5 views
1

jQuery를 사용하여 표준 기능 (예 : alertconfirm)을 덮어 쓰려고합니다. 이를 위해JavaScript/jQuery를 사용하여 실행을 "일시 중지"하고 사용자 입력 대기

, 내가 경고 또는 확인이 코드를 요청하는 대화 상자를 보여줍니다 뭔가를 구축하고, 같은이 보인다 :

function throwError(e_content) { 
    var e_title = "Error"; 
    var e = errordiv; 
    var return_error = e.start + e.header_start + e_title + e.header_end + e.body_start + e_content + e.body_end + e.end; 
    jQuery("body").append(return_error); 
    return; 
} 

function throwConfirm(e_content) { 
    function confirmCallback() { 
     var retval = "213"; 
     jQuery("body").on("click", function (e) { 
      if (e.target.id == "confirm_okay") { 
       hideError(); 
       retval = true; 
      } 
      else if (e.target.id == "confirm_cancel") { 
       hideError(); 
       retval = false; 
      } 
     }) 

     retval == "213" ? confirmCallback() : retval; 
     return retval; 
    } 

    var e_title = "Confirm Action"; 
    var e = confirmdiv; 
    var return_error = e.start + e.header_start + e_title + e.header_end + e.body_start + e_content + e.body_end + e.end; 
    jQuery("body").append(return_error); 

    return confirmCallback(); 
} 

function hideError() { 
    jQuery(document).find(".custom_error_div").remove(); 
    return; 
} 

var errordiv = { 
    start: '<div class="custom_error_div"><div class="custom_error_div_content" >', 
    end: '</div></div>', 

    header_start: '<div class="custom_error_div_header">', 
    header_end: '</div>', 

    body_start: '<div class="custom_error_div_body">', 
    body_end: '<div class="bottom-buttons">' 
    + '<button id="alert_okay">Ok</button>' 
    + '</div>' 
}; 

var confirmdiv = { 
    start: '<div class="custom_error_div"><div class="custom_error_div_content" >', 
    end: '</div></div>', 

    header_start: '<div class="custom_error_div_header">', 
    header_end: '</div>', 

    body_start: '<div class="custom_error_div_body">', 
    body_end: '<div class="bottom-buttons">' 
    + '<button id="confirm_okay">Ok</button><button id="confirm_cancel">Cancel</button>' 
    + '</div>' 
}; 
이의 기초가 작동

, 그것은 경고하고 확인한다 내가 원할 때 - 확인이 문제가 될 때 "예"또는 "아니오"를 클릭하면 throwConfirm 함수가 true 또는 false를 반환한다고 가정하고 대신이 상황이 발생하지 않는 것으로 보입니다. 대신 최대 호출에 도달합니다. 크기.

이 복잡하고 복잡한 손의 질문에 관련이없는 일을 뒤에 기초 - 내가 알아야 할 사항은 다음과 같습니다
나는 어떤 버튼을 사용자가 클릭에 따라 true 또는 false를 반환하는 throwConfirm 기능을 얻는 방법 ?

+2

을 받고 어떤 오류 메시지 콜백을 설정하는 대신 사용하는 더 나은 방법이있을거야? 문제를 보여줄 수 있도록이 코드를 작업 스 니펫으로 만들 수 있습니까? –

+0

콜백 함수를'throwConfirm'에 전달하고'confirmCallback'에서 실행하여 "return"값을 전달해야합니다. – Archer

+0

최대 호출 크기가 다음과 같이 나타납니다 :' "213"? confirmCallback() : retval;'. onclick 함수가 호출되지 않고 방금 생성되었습니다. 따라서 retval은 항상 "213"이 될 것이므로 여러분은 영원히 재귀합니다. 값을 반환하려는 대신 전역 변수를 설정하거나 콜백 함수를 호출하십시오. 조금 더 html을 게시 하시겠습니까? e.start + e.header_start + e_title + e.header_end + e.body_start + e_content + e.body_end + e.end' – Tibrogargan

답변

1

이렇게하는 한 가지 방법을 보여줍니다. 그것은 일종의 미봉책 - 당신은 글로벌 _callback 변수

"use strict"; 
 

 
function throwError(e_content) { 
 
    var e_title = "Error"; 
 
    var e = errordiv; 
 
    var return_error = e.start + e.header_start + e_title + e.header_end + e.body_start + e_content + e.body_end + e.end; 
 
    jQuery("body").append(return_error); 
 
    return; 
 
} 
 

 
function throwConfirm(e_content, callback) { 
 
    hideError(); 
 
    var e_title = "Confirm Action"; 
 
    var e = confirmdiv; 
 
    var return_error = e.start + e.header_start + e_title + e.header_end + e.body_start + e_content + e.body_end + e.end; 
 
    jQuery("body").append(return_error); 
 
    // this is a hack, but I'm tired 
 
    _callback = callback 
 
} 
 

 
function hideError() { 
 
    jQuery(document).find(".custom_error_div").remove(); 
 
    return; 
 
} 
 

 
var errordiv = { 
 
    start: '<div class="custom_error_div"><div class="custom_error_div_content" >', 
 
    end: '</div></div>', 
 

 
    header_start: '<div class="custom_error_div_header">', 
 
    header_end: '</div>', 
 

 
    body_start: '<div class="custom_error_div_body">', 
 
    body_end: '<div class="bottom-buttons">' 
 
    + '<button id="alert_okay">Ok</button>' 
 
    + '</div>' 
 
}; 
 

 
var confirmdiv = { 
 
    start: '<div class="custom_error_div"><div class="custom_error_div_content" >', 
 
    end: '</div></div>', 
 

 
    header_start: '<div class="custom_error_div_header">', 
 
    header_end: '</div>', 
 

 
    body_start: '<div class="custom_error_div_body">', 
 
    body_end: '<div class="bottom-buttons">' 
 
    + '<button id="confirm_okay">Ok</button><button id="confirm_cancel">Cancel</button>' 
 
    + '</div>' 
 
}; 
 

 
var _callback; 
 
function init() { 
 
    jQuery("body").on("click", function (e) { 
 
     if (e.target.id == "confirm_okay") { 
 
      hideError(); 
 
      _callback(true); 
 
     } 
 
     else if (e.target.id == "confirm_cancel") { 
 
      hideError(); 
 
      _callback(false); 
 
     } 
 
    }) 
 
    $(document).on("click", "#foo", function() { throwConfirm("bar", doSomethingWithMyRetval) }); 
 
} 
 

 
function doSomethingWithMyRetval(foo) { 
 
// foo foo foo 
 
console.log("The retval from the confirm was: "+foo); 
 
} 
 
$(document).ready(init);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
 
<body> 
 
<input id="foo" type="button"> 
 
</body>

+0

매우 흥미 롭습니다 ... 이걸 갖고 놀아보고 내 페이지에서 필요한 것이 있는지 확인하십시오. 감사합니다! :) –

+0

@SamSwift 웃기 jQuery는'on()'을 호출 할 때 새로운 핸들러를 추가하기 때문에 이전 버전이 여러 번 로깅됩니다. 이 버전은 그것을 수정합니다. - 그러나 나는 하나의 콜백 핸들러를 생성하는'on()'버전이 있다고 믿습니다. 단지 구문을 기억하지 못합니다. _callback kludge 대신에 이런 식으로하는 것이 더 낫다. – Tibrogargan

+0

오,'one()'으로 할 수 있었다. – Tibrogargan

관련 문제