2016-07-27 108 views
0

我使用Grails 3.1.9作爲平臺,我的問題是當按鈕添加項目沒有被點擊時,我可以看到對話框的內容框放在頁面底部,點擊按鈕時,內容從底部消失。我如何防止這種情況發生?任何幫助你可以提供將不勝感激。Div對話框的內容甚至在Jquery Dialog未打開時顯示

之前單擊Add Item按鈕 This is before clicking Add Item Button

單擊添加項目按鈕 This is After Clicking Add Item Button

show.gsp代碼之後是:

<div id="dialogEntry" title="Item Entry"> 
       <fieldset class="form"> 
        <form id="entryForm" action="" method="post" ><input type="hidden" name="_method" value="PUT" id="_method" /> 
         <input type="hidden" name="invoice.id" value="${invoice.id}" /> 
         <div class="fieldcontain required"> 
         <label for="product"> 
          <g:message code="orderItem.product.label" default="Product" /> 
          <span class="required-indicator">*</span> 
         </label> 
         <input type="text" name="product" value="" required="" id="product" /> 
         <input type="hidden" id="prodid" value="" /> 
         <div class="fieldcontain"> 
          <label for="quantityInStock"> 
           Quantity in Stock 
          </label> 
          <input type="text" id="quantityInStock" value="" readonly="true" /> 
         </div> 
         </div> 
         <div class='fieldcontain required'> 
         <label for='quantity'>Quantity 
         <span class='required-indicator'>*</span> 
         </label><input type="number" name="quantity" value="1" required="" min="1" id="quantity" /> 
         </div> 
         <div class='fieldcontain required'> 
         <label for='price'>Price 
         <span class='required-indicator'>*</span> 
         </label><input type="number" name="price" value="" required="" step="0.01" min="1.00" id="price" /> 
         </div> 
         <div class="fieldcontain"> 
          <label for="totalAmount"> 
           Total Amount 
          </label> 
          <input type="null" name="totalAmount" value="" id="totalAmount" /> 
         </div> 
        </form> 
       </fieldset> 
      </div> 
<script> 

      var editId; 
      document.getElementById("add").onclick = function() {myFunction()}; 
      function myFunction() { 
       document.getElementById("add").innerHTML = 
         $("#dialogEntry").dialog({ 
          autoOpen: true, 
          modal: true, 
          width: 500, 
          buttons: [ 
           { 
            text: "Save", 
            click: function() { 
             var quantity = $('#quantity')[0].value; 
             var quantityInStock = $('#quantityInStock')[0].value; 
             if (quantity.length == 0) { 
              alert('Quantity is required'); 
              $('#quantity')[0].focus(); 
              return; 
             } 
             if (parseInt(quantity) > parseInt(quantityInStock)) { 
              alert('Quantity cannot be served as Quantity in Stock is just ' + quantityInStock); 
              $('#quantity')[0].focus(); 
              return; 
             } 
             $(this).dialog("close"); 
             var price = $('#price')[0].value; 
             var prodid = $("#prodid")[0].value; 
             // submit to server 
             //var form = $('#entryForm')[0]; 
             if (editId != 0) { 
              $.ajax({ 
               type: "POST", 
               url: "${resource(dir:'orderItem/updatex')}/" + editId, 
               data: {'productid':prodid, 'quantity':quantity, 'price':price}, 
               async: true, 
               cache: false, 
               success: function (result) { 
                //alert('OK ' + result.success.message) 
                update(editId) 
               }, 
               error: function (XMLHttpRequest, textStatus, errorThrown) { 
                alert(textStatus + " " + errorThrown); 
               } 
              }); 
             } else { 
              $.ajax({ 
               type: "POST", 
               url: "${resource(dir:'orderItem/savex')}/" + editId, 
               data: {'productid':prodid, 'quantity':quantity, 'price':price, 'invoiceid':${invoice.id}}, 
               async: true, 
               cache: false, 
               success: function (result) { 
                var id = result.success.id 
                //alert('OK ' + id) 
                update(id) 
               }, 
               error: function (XMLHttpRequest, textStatus, errorThrown) { 
                alert(textStatus + " " + errorThrown); 
               } 
              }); 
             } 
            } 
           }, 
           { 
            text: "Cancel", 
            click: function() { 
             $(this).dialog("close"); 
            } 
           } 
          ] 
         }); 
      } 
</script> 

回答

1
<div id="dialogEntry" title="Item Entry"> 

更改爲:

<div id="dialogEntry" title="Item Entry" style="display:none;"> 

更改此:

document.getElementById("add").innerHTML = 
         $("#dialogEntry").dialog({ 

document.getElementById("add").innerHTML = 
         $("#dialogEntry").show().dialog({ 

更改爲:

text: "Cancel", 
            click: function() { 
             $(this).dialog("close").hide(); 
            } 
+0

雖然上面應該有希望的工作。如果有可能,你應該看看有一箇中央對話框,也許你的gsp中包含對話框的模板。內容應該被動態添加到對話框中。比上述方法更復雜但更整潔 – Vahid

+0

謝謝!這是一個簡單而快速的解決方案!它工作完美。至於有一箇中央對話框,我將來會研究它,我仍然在用gsp進行編程。:) –

+0

這將是一個很好的開始:http://stackoverflow.com/questions/3694693/how -to-have-jqueryui-dialog-box-dynamic-load-content - 基本上,按照這個http://stackoverflow.com/questions/37420897/sending-an-error-in-grails-and-reading-it-in -javascript來讓jquery將內容加載到相關的div中。 – Vahid

相關問題