jQueryã§submitã¤ãã³ããä¸æ¦æ¢ãã¦ãéåæãã¦ã¾ãåããã®é£ãã
$('form').submit(function(){ $.getJSON('url') .done(function(result){ if (result) { //ãã®å ´åã¯submitããã(A) } //ãã®å ´åã¯submitããªã(B) }) .fail(function(jqJHR){ //失æãããsubmitããªã(B) }); //ä¸æ¦submitã¤ãã³ãã¯æ¢ãã(C) e.preventDefault(); });
ãããªæãã§ãsubmitã¤ãã³ããæ¾ã£ã¦ããã®ä¸ã§éåæéä¿¡ãã¦ããã®çµæ(A)ã®å ´åã¯submitããããã$.getJSON()
ã¯éåæãªã®ã§done()
ãfail()
*1ã«å¦çã移ãæã«ã¯submitã®å¦çã¯é²ãã§ãã¾ãã¾ãããªã®ã§ã(C)ã§e.preventDefault()
*2ã§submitã¤ãã³ããæ¢ãã¦ããå¿
è¦ãããã¾ããã§ã$.getJSON()
ã®çµæã«ãã£ã¦ã¯ãsubmitãç¶ç¶ãããããã§ãããã¤ãã³ããä¸æ¦æ¢ãã¦ãã£ã±ãåããæ¹æ³ãåãããã¯ã¾ãã¾ããã
ä¸å¿ã®è§£æ±ºç
var onSubmit = function(){ $.getJSON('url') .done(function(result){ if (result) { //ãã®å ´åã¯submitããã(A) $('form').unbind('submit', onSubmit); $('form').submit(); } //ãã®å ´åã¯submitããªã(B) }) .fail(function(jqJHR){ //失æãããsubmitããªã(B) }); //ä¸æ¦submitã¤ãã³ãã¯æ¢ãã(C) e.preventDefault(); }); $('form').bind('submit', onSubmit);
bindããå½¢ã«ãã¦ããã¦ã(A)ã®ã¨ãã§submitãããªã¬ã¼ã§åããã¾ããããã ã¨submitã¤ãã³ãã®ç¶ãã§ã¯ãªããããä¸åº¦submitã¤ãã³ããçºç«ãããã®ã§ãã«ã¼ããã¦ãã¾ãäºã«ããªã®ã§ãç´åã«unbindããã¨è¨ããã¡ãã£ã¨ç¡çãããªæ¹æ³ã§ããe.preventDefault()
ã§æ¢ããã¤ãã³ãããåããã¡ã½ããã¨ãç¡ããã ãããã
ã¡ãªã¿ã«(B)ã®ã¨ãã¯ãããããsubmitã¤ãã³ãã(C)ã§æ¢ãããã¦ãã®ã§ãä½ãããªãã¦ã大ä¸å¤«ã§ãã
jQueryã®event.preventDefault()
IEã¯e.preventDefault()
ãç¡ãã代ããã«e.returnValue = false
ãããã¨ããç¬èªä»æ§ã§ããæ®æ®µã¯e.preventDefault()
ãããªãã£ã¦return false
ã°ãã使ã£ã¦ãã®ã§ãæ°ã«ãªãã¾ããã
ä»åe.preventDefault()
ã使ã£ãã®ã¯e
ãªãã¸ã§ã¯ãã«æ¢ããã¤ãã³ããåéãããã¡ã½ãããããããã¨æã£ãåæ®ã ã£ããã
preventDefault: function() { this.isDefaultPrevented = returnTrue; var e = this.originalEvent; if ( !e ) { return; } // if preventDefault exists run it on the original event if ( e.preventDefault ) { e.preventDefault(); // otherwise set the returnValue property of the original event to false (IE) } else { e.returnValue = false; } },https://github.com/jquery/jquery/blob/1.6/src/event.js#L611
jQueryå
ã§ã¯jQueryã®Eventãªãã¸ã§ã¯ãã§ãã¡ããã¨IEç¨ã®e.returnValue = false
ãå
¥ã£ã¦ã¾ããã
ç°å¢
jQuery | 1.5.1 1.6.1 |