如何在彈出窗口中顯示錯誤消息,並且如果成功消息隱藏所有表單字段並顯示成功消息。如何在彈出窗口中顯示錯誤消息jquery
目前我正在這樣做: 在窗體上方放置成功消息並在窗體關閉後關閉它。 錯誤消息div放置在窗體的上方,即在成功div內。請建議我最好的方法。給一些示例代碼,以便我可以實現這一點。
我的html代碼:
<div id="successMessage">
<form method="POST" id="update_form" onsubmit = "return false;">
<div id="errorMessage"></div>
<table align="center">
<tr>
<td align="right">First Name:</td>
<td align="left"><input type="text" name="firstName" value="<?php echo $firstName; ?>" />
</td>
</tr>
<tr>
<td align="right">Last Name:</td>
<td align="left"><input type="text" name="lastName" value="<?php echo $lastName; ?>" />
</td>
</tr>
<tr>
<td align="right" colspan="2">
<input type="hidden" name="userId" value="<?php echo $userId; ?>" />
<input type="button" name="updateUserDetails" value="Update" onclick="updateUserDetails();">
</td>
</tr>
</table>
</form>
</div>
jQuery代碼:
function editUserDetails(userId, operation){
currentOperation = operation;
$('#editUserDetails').dialog('open');
$('#editUserDetails').html("Loading...");
$.ajax({
type: "POST",
url: "editUserDetails.php?userId="+userId,
data: "",
success: function(msg){
$('#editUserDetails').html(msg);
},
error:function(msg){
//alert(msg);
$('#editUserDetails').dialog("close");
}
});
}
function updateUserDetails(){
$.ajax({
type: "POST",
url: "updateUserDetails.php",
data: $("#update_form").serialize(),
success: function(msg){
if(msg=="Updated Successfully")
{
$('#successMessage').html(msg);
}
else
{
$('#errorMessage').html(msg);
}
},
error:function(msg){
$('#editUserDetails').dialog("close");
}
});
});
});
在我看來,你應該單獨形式和消息,所以消息的佈局是獨立於形式的。 (IE。你稍後想要一些奇妙的警告框樣式或使用驗證插件)。偉大的結構,不要限制元素。 – 2012-03-20 07:17:02