2013-02-01 46 views
4

我有一個模式窗體,用戶可以重置其密碼。關閉時重置模態對話框

<div id="password-form" title="Reset Password"> 
<form method="post"> 
<fieldset> 
<label for="emailreset">Enter Email Address</label> 
<input type="text" name="emailreset" id="emailreset" class="text ui-widget-content ui-corner-all" /> 
</fieldset> 
</form> 
</div> 

當用戶點擊重置密碼按鈕時,我調用一個函數來檢查用戶是否存在。 在發佈成功後,我將div的內容更改爲生成的消息。 所有這些都按預期工作。現在我想要發生的事情是一旦關閉了模式對話框,我想讓內容重置回表單。發生這種情況我遇到了問題。我已經嘗試了不同的東西,但它沒有任何幫助,將不勝感激。

這裏就是我對jQuery的提前

$("#password-form").dialog({ 
autoOpen: false, 
height: 200, 
width: 300, 
modal: true, 
buttons: { 
"Reset Password": function() { 

$.ajax({ 
url: "/model/websitemanager.cfc" 
, type: "post" 
, dataType: "html" 
, data: { 
method: "resetpassword"   
, emailreset: $("#emailreset").val() 
} 
, success: function (data){ 
//alert(data); 
$('#password-form').html('<p>'+data+'</p>'); 
} 
// this runs if an error 
, error: function (xhr, textStatus, errorThrown){ 
// show error 
alert(errorThrown); 
} 
}); 
<!--//end ajax call//--> 
}, 
}, 
close: function() { 
emailreset.val("").removeClass("ui-state-error"); 
$(this).dialog('destroy').remove() 
} 
}); 

謝謝!

周杰倫

+0

你可以只使用.reset段(),只需先給表單一個ID。 http://stackoverflow.com/questions/6653556/jquery-javascript-function-to-clear-all-the-fields-of-a-form – PlantTheIdea

+0

這將工作,如果我重置表單。但形式被成功的內容取代。所以當對話框關閉並再次打開時,我想要顯示錶單。 – user2033361

+0

關閉對話框我最後只是重新創建表格: $('#password-form')。html('

< input type =「text」name =「emailreset」id =「emailreset」class =「text ui-widget-content ui-corner-all」/>
'); 如果有人知道更好的選擇讓我知道 – user2033361

回答

2

這裏有一個簡單的功能我有全局可用在我的應用程序:

function clearInputs(target) { 
    $(target).find(':input').each(function() { 
    switch (this.type) { 
     case 'password': 
     case 'select-multiple': 
     case 'select-one': 
     case 'text': 
     case 'textarea': 
      $(this).val(''); 
      break; 
     case 'checkbox': 
     case 'radio': 
      this.checked = false; 
    } 
    }); 
} 

這裏就是我如何使用它的jQuery的對話框:

$('#myDialog').dialog({ 
    buttons:{ 
     'Submit':function() { 
      // Do something here 
      clearInputs($(this).find('form')); 
      $(this).dialog('close'); 
     }, 
     'Cancel':function() { 
      clearInputs($(this).find('form')); 
      $(this).dialog('close'); 
     } 
    }, 
    close:function() { 
     clearInputs($(this).find('form')); 
    } 
}); 
+0

@ user2033361 - 讓我知道這是否有助於回答你的問題! – Aegix