我想使用ajax修改我的表数据,但是onclick()事件不会触发任何事情。这是代码。 (onclick()事件写在最后一个div标签中)
@{
ViewBag.Title = "Edit_Room";
Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}
<script src="~/Content/Scripts/jquery-3.1.1.js"></script>
<script src="~/Content/Scripts/edtiRoom.js"></script>
<h2>Edit_Room</h2>
<div>
<div class="panel panel-default container">
<div class="panel-body">
<h2>Please enter required information</h2>
<div class="col-num2">
<div>
<h3>Room Number</h3>
<select name="roomid" id="roomid">
@foreach (var item in Model)
{
<option value="@item.Id">@item.Id</option>
}
</select>
</div>
<div>
<h3>Room Type</h3>
<input type="text" id="roomtype" name="type" pattern="[a-zA-Z]+" />
</div>
<div>
<h3>Price</h3>
<input type="tel" id="price" name="price" pattern="^[0-9]*$" />
</div>
<div>
<button type="button" onclick="editRoomFeatures()" class="btn btn-primary btn-lg">
Update
</button>
</div>
</div>
</div>
</div>
</div>
这是我想在按钮点击时调用的函数(用edtiRoom.js文件编写)。
var editRoomFeatures = function() {
var roomid = $("#roomid").find('option:selected').text();
var roomtype = $("#roomtype").val();
var roomprice = $("#price").val();
$.ajax({
url: "Admin/Edit_Room_Features/",
type: "POST",
data: { roomid: roomid, roomtype: roomtype, roomprice: roomprice }
})
.done(function() {
alert("Success");
//alert(temp);
//$("#" + temp).html(partialViewResult);
});
};
这&#34; editRoomFeatures&#34;然后将在&#34; Admin&#34;中调用此功能。控制器。
public JsonResult Edit_Room_Features(string roomid,string roomtype,string roomprice)
{
//do something here
}
但是当我调试我的项目时,按钮onclick上没有任何反应。
更新:我已经能够自己解决这个问题了。
答案 0 :(得分:1)
function editRoomFeatures() {
var roomid = $("#roomid").find('option:selected').text();
var roomtype = $("#roomtype").val();
var roomprice = $("#price").val();
$.ajax({
url: "Admin/Edit_Room_Features/",
type: "POST",
data: { roomid: roomid, roomtype: roomtype, roomprice: roomprice }
})
.done(function() {
alert("Success");
//alert(temp);
//$("#" + temp).html(partialViewResult);
});
}
不需要包装
<button type="button" onclick="editRoomFeatures" class="btn
btn-primary btn-lg">
Update
</button>
答案 1 :(得分:0)
将您的功能保留在$(文件).ready之外。它会起作用。