Javascript修改为允许警报
本文关键字:许警报 修改 Javascript | 更新日期: 2023-09-27 18:10:47
我的javascript经验非常有限,我正试图修改下面的脚本,以在POST尝试后显示成功或失败的警报:
<script language="javascript" type="text/javascript">
$(function() {
$("form").submit(function(e) {
$.post($(this).attr("action"),
$(this).serialize(),
function(data) {
$("#result").html(data);
});
e.preventDefault();
});
});
</script>
我知道它需要看起来像这样:
success: function(){
alert("Edit successful");
},
error: function(){
alert('failure');
但无法找出如何在没有语法错误的情况下集成它。这个脚本用于执行POST,上面的代码通常会执行POST,以允许发生成功或失败的确认:
@using (Html.BeginForm("Admin", "Home", "POST"))
{
<div class="well">
<section>
<br>
<span style="font-weight:bold">Text File Destination:   </span>
<input type="text" name="txt_file_dest" value="@ViewBag.one">
<span style="color:black">   Example:</span><span style="color:blue"> ''pathpart1''pathpart2$''</span>
<br>
<br>
<span style="font-weight:bold">SQL Connection String:</span>
<input type="text" name="sql_Connection" value="@ViewBag.two">
<span style="color:black">   Example:</span> <span style="color:blue"> Server=myserver;Database=mydatabase;Uid=myusername;Pwd=mypswd</span>
<br>
<br>
<button class="btn btn-success" type="submit" name="document">Save Changes</button>
</section>
</div>
}
ActionResult
public ActionResult Admin(string txt_file_dest, string sql_Connection)
{
AdminModel Values = new AdminModel();
if (txt_file_dest != null)
{
Values.SAVEtxtDestination(txt_file_dest);
}
if (sql_Connection != null)
{
Values.SAVEsqlConnection(sql_Connection);
}
ViewBag.one = Values.GetTextPath();
ViewBag.two = Values.GetSqlConnection();
return View();
}
$.post(
$(this).attr("action"), // url
$(this).serialize(), // data
function(data) { //success callback function
alert("Edit successful");
}).error(function() {
alert('failure');
});
这是一个简写的ajax函数,可以写成:
$.ajax({
type: "POST",
url: $(this).attr("action"),
data: $(this).serialize(),
success: function(data){
alert("Edit successful");
},
error(function() {
alert('failure');
}
});