2011-04-03 61 views
8

我有一個表單裏面的jQuery UI模式對話框,當我點擊提交按鈕沒有任何反應。有沒有辦法在jQuery模式對話框中提交表單,而不必在JavaScript中編碼按鈕?你可以提交一個jQuery的模式對話框內的表格

這是我的代碼片段;

<script> 
    // increase the default animation speed to exaggerate the effect 

    $(function() { 
     $("#dialog").dialog({ 
      autoOpen: false, 
      draggable: false, 
      resizable: false, 
        modal: true, 

     }); 

     $("#opener").click(function() { 
      $("#dialog").dialog("open"); 
      return false; 
     }); 
    }); 

    function SetValues() 
    { 
     var s = 'X=' + window.event.clientX + ' Y=' + window.event.clientY ; 
     document.getElementById('divCoord').innerText = s; 

    } 
    </script> 

<div id="dialog" title="new task"> 
     <form method="post" action="/projects/{{ project.slug }}/"> 
      <div class="form-row"> 
       <label class="required" for="title">task type</label> 
       <select name="type"> 
       {% for TaskType in project.tasktype_set.all %} 
        <option value="{{ TaskType.name }}">{{ TaskType.name }}</option> 
       {% endfor %} 
       </select> 
      </div> 
      <div id="buttons"> 
       <input type="submit" value="create"/> 
      </div> 
     </form> 
</div> 

<button id="opener">new task</button> 

如果我刪除「模態:真」,使對話框非模態,它將提交。

+0

相關http://stackoverflow.com/questions/4743751/jquery-modal-dialog-box-isnt-submitting-my-form – 2011-04-03 06:52:50

+0

哈伊姆,我有看到這篇文章 - 略有不同的情況,因爲在提供的URL提供的jQuery對話框只是確認提交操作,而我的表單是模式對話框的一部分。 我也嘗試過這個,但沒有成功(它也不會提交我的表單)。\ – Jamie 2011-04-03 07:41:16

回答

6

,你可以使用$。員額()或$。阿賈克斯()你的對話框如中:

$("#dialog").dialog({ 
    autoOpen: false, 
    draggable: false, 
    resizable: false, 
    modal: true, 
    buttons: { 
     "Save": function() { 
      var type = $("#type option:selected").val(); 
      $.post("/projects/{{project.slug}}/", {type:type}, function(data) { 
       alert(data);// ---> data is what you return from the server 
      }, 'html'); 
     }, 
     "Close": function(){ 
      $(this).dialog('close'); 
     } 

    } 
}); 

只是給你的選擇標籤的ID來......使得它更容易拉值。也擺脫了輸入按鈕,因爲現在你將有對話框中的保存按鈕。

+1

嗨Ziad,謝謝你的迴應。這讓我更加接近。服務器返回一個我應該重定向到的頁面。關於如何從對話框中重定向到此的任何想法? – Jamie 2011-04-03 11:23:02

+1

@Jamie:在成功函數中使用'window.location = Kamyar 2011-04-03 11:36:11

+0

Kamyar你是對的,海梅;替換「警報(數據)」;「與Kamyar的建議代碼。 – 2011-04-03 22:16:40

2

您必須將對話框移回到您的表單中。創建對話框後執行此操作。像:

$("#dialog").parent().appendTo($("form:first")); 

UPDATE: 爲什麼被包含在你的表格對話框?你有兩種形式?嘗試將表單移動到容器中。下面是完整的代碼,我認爲應該工作:

<script> 
// increase the default animation speed to exaggerate the effect 

$(function() { 
    $("#dialog").dialog({ 
     autoOpen: false, 
     draggable: false, 
     resizable: false, 
       modal: true, 

    }); 

    $("#dialog").parent().appendTo($("form:first")); 

    $("#opener").click(function() { 
     $("#dialog").dialog("open"); 
     return false; 
    }); 
}); 

function SetValues() 
{ 
    var s = 'X=' + window.event.clientX + ' Y=' + window.event.clientY ; 
    document.getElementById('divCoord').innerText = s; 

} 
</script> 

<form id="form1" method="post" action="/projects/{{ project.slug }}/"> 
    <div id="dialog" title="new task"> 
     <div class="form-row"> 
      <label class="required" for="title">task type</label> 
      <select name="type"> 
      {% for TaskType in project.tasktype_set.all %} 
       <option value="{{ TaskType.name }}">{{ TaskType.name }}</option> 
      {% endfor %} 
      </select> 
     </div> 
     <div id="buttons"> 
      <input type="submit" value="create"/> 
     </div> 
    </div> 
</form> 

<button id="opener">new task</button> 
+0

我嘗試添加以下內容, $(「#opener」).click(function(){' '$(「#dialog」).dialog(「open」);' '$(「#dialog」)。parent()。appendTo ($(「form:first」));' 'return false;' '});' 不幸 - 屏幕變灰,但對話從不出現。 – Jamie 2011-04-03 11:08:51

+0

嘗試爲表單添加一個id屬性:'

'並移動$(「#dialog」)。parent()。appendTo($(「form:first」));'before '$(「#opener」)。click ...' – Kamyar 2011-04-03 11:26:18

+0

嗨Kamyar,我試過你的代碼建議。該對話框加載,但是當我點擊'創建'按鈕仍然沒有任何反應。 – Jamie 2011-04-03 11:55:28

相關問題