尝试构建脚本文件以防止表单的默认操作。当点击搜索按钮时,假设显示“加载”指示符图像,并且假设表格出现在按钮下方。我不能让ajax“加载”指示图像出现,也不能表格。有人可以帮帮我吗?这是我的代码:
EquipmentScript.js
$(function () {
$("#equipmentSearch").submit(function (event) {
event.preventDefault();
var form = $(this);
$.ajax({
url: form.attr("action"),
data: form.serialize(),
beforeSend: function () {
$("#ajax-loader").show();
},
complete: function () {
$("#ajax-loader").hide();
},
success: function (data) {
var html = Mustache.to_html($("#itemTemplate").html(),
{db.Equipment});
}
});
});
});
Index.cshtml
<div class="panel panel-default">
<div class="panel-body">
<!--Index method in the EquipmentModel controller-->
<form id="equipmentSearch" method="get" action="@Url.Action("Index", "EquipmentModel")">
<button id="process" value="search" class="btn btn-info">Search For Equipment</button>
<img id="ajax-loader" src="@Url.Content("~/Images/ajax-loader.gif")" style="display:none" />
</form>
<script id="equipmentTemplate" type="text/html">
</script>
<div id="searchresults"></div>
</div>
EquipmentModel Controller中的Index方法如下:
public ActionResult Index()
{
return View(db.Equipments.ToList());
}