2013-07-18 32 views
0

我想在jQuery中編寫if else語句。如果點擊該按鈕,則禁用該按鈕,直到動作完成,然後僅啓用。這是我目前擁有的代碼。當我導航到頁面時,操作不停地循環。由於我還不熟悉jQuery,請耐心等待。使用jquery禁用按鈕,並在使用jQuery完成操作後啓用

僅供參考,我使用primefaces COMMANDBUTTON

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3c.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:p="http://primefaces.org/ui" 
    xmlns:hx="http://www.ibm.com/jsf/html_extended"> 
<h:head> 
    <title>ma1009.xhtml</title> 
    <meta http-equiv="keywords" content="enter,your,keywords,here" /> 
    <meta http-equiv="description" 
    content="A short description of this page." /> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 

    <script src="../../jquery-1.9.1.min.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 

      if ($("#button").click()) { 
       $("#button", this).prop('disabled', true); 
      } else { 
       $("#button", this).prop('disabled', false); 
      } 

     }); 
    </script> 
</h:head> 

<h:body> 
    <h:form id="form1" enctype="multipart/form-data" prependId="false"> 

     <p:commandButton id="button" type="submit" value="Submit" 
      action="#{pc_Ma1009.doSubmitAction}" ajax="false"></p:commandButton> 

    </h:form> 
</h:body> 
</html> 
+2

看來你的代碼是完全錯誤的,告訴我什麼樣的動作是打算完成的? –

+0

@ABFORCE the action =「#{pc_Ma1009.doSubmitAction}」。這將需要大約15秒才能完成。 – hahagal

+0

我認爲Alexey Aza的答案對你很有幫助,你應該在'Alexey Aza'的指定位置做你的動作回答 –

回答

0

我設法用下面的代碼來得到它。 @ABFORCE感謝您的幫助! ^^ @Alexey Aza感謝您的幫助! ^^

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3c.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:p="http://primefaces.org/ui" 
    xmlns:hx="http://www.ibm.com/jsf/html_extended"> 
<h:head> 
    <title>ma1009.xhtml</title> 
    <meta http-equiv="keywords" content="enter,your,keywords,here" /> 
    <meta http-equiv="description" 
     content="A short description of this page." /> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 

    <link type="text/css" rel="stylesheet" 
     href="#{request.contextPath}/theme/primefaces-aristo/theme.css" /> 

    <script src="../../jquery-1.9.1.min.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $("button").click(function() { 
       $(this).css('background', 'red'); 
       $(this).addClass('ui-state-disabled'); 
      }); 
     }); 
    </script> 
</h:head> 

<h:body> 
    <h:form id="form1" enctype="multipart/form-data" prependId="false"> 

     <p:commandButton id="button" type="submit" value="ma1009" styleClass="commandButton" 
      action="#{pc_Ma1009.doSubmitAction}" widgetVar="ajaxStatusDialog"> 
      <p:ajaxStatus onstart="ajaxStatusDialog.show();" onsuccess="ajaxStatusDialog.hide();"></p:ajaxStatus> 

     </p:commandButton> 

    </h:form> 
</h:body> 
</html> 
2

應該是這樣的:

$(document).ready(function(e) { 
    e.preventDefault(); 
    $("#button").click(function(){ 
     $(this).prop("disabled", true); 
     //your action (maybe function pc_Ma1009.doSubmitAction()), look at your html generated by <p:commandButton id="button" 
     $(this).prop("disabled", false); 
    }); 

}); 
+0

爲什麼'e.preventDefault();'? –

+0

沒有注意到它是提交 –

+0

ups ...你是對的:) –