2011-04-15 91 views
2


我想用jQuery對話框做一個搜索表單。請參閱下面的完整程序。我有一臺服務器在我的電腦上運行,並且http://localhost/search。有幾點懷疑jquery對話框:表單提交

  1. ajax調用有些問題,因爲我收到一條錯誤消息。我也嘗試了GET請求。
  2. 在Chrome中,服務器上的請求類型爲POST,但在Iceweasel(Firefox)中爲選項
  3. 在Iceweasel(Firefox)中,模式窗口保持打開狀態並顯示錯誤。但在谷歌瀏覽器中,模式窗口關閉。這可能與下面的JavaScript代碼有什麼不妥有關。我不是JS的專家。

非常感謝,
bsr。

<html><head> 
     <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" /> 
    </head> 
    <body> 

     <input id="myButton" name="myButton" value="Click Me" type="button" /> 

     <div id="myDiv" style="display:none">  
     <form id="myform"> 
      <input name="q" placeholder="Search .."> 
     </form> 
     </div> 

     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
     <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js"></script> 

     <script type="text/javascript"> 
      jQuery(document).ready(function(){  
       jQuery("#myButton").click(showDialog); 
        $myWindow = jQuery('#myDiv'); 
        $myWindow.dialog({ width: 400, autoOpen:false, title:'Hello World', 
          overlay: { opacity: 0.5, background: 'black'}, 
          buttons: { 
        "Submit Form": function() { $('form#myform').submit();}, 
        "Cancel": function() {$(this).dialog("close");} 
        } 
          }); 
        }); 

     var showDialog = function() { 
      $myWindow.show(); 
      $myWindow.dialog("open"); 
      } 
     var closeDialog = function() { 
      $myWindow.dialog("close"); 
     } 

     var successFn = function (response) {   
       $("#myform").parent().html('').html(response); 
       } 
     var errorFn = function (xhr, ajaxOptions, thrownError){ 
         $("#myform").parent().html('').html(xhr.statusText); 
         } 

     $('form#myform').submit(function(){ 
      $.ajax({ 
      type: 'post', 
      dataType: 'html', 
      url: '/search', 
      async: false, 
      data: $("#myform").serialize(), 
      success: successFn, 
      error: errorFn 
      }); 
     });  

     </script> 
    </body></html> 

回答

2

你可能需要這個。

// include modal into form 
$(this).parent().appendTo($("form:first")); 

編輯:

有了這個,你添加到對話的形式,從而可以回傳

,你可以在打開事件中添加這一點。

嘗試使用json作爲dataType,jSon是一個了不起的工具,其中ajax =)