我創建了資料表,我在每一行中添加了復選框并將下拉串列添加到一列,效果很好,我在頂部只添加了一個提交按鈕。所以基本上我在這里嘗試的是用戶可以選擇復選框并從行中選擇下拉選單。一旦提交選定的行必須更新。
這是我當前的代碼: 在視圖中:
<input type="button" id="delete" value="Submit" />
<table id="example" cellpadding="10" width="100%">
<thead>
<tr>
<th><input id="checkAll" type="checkbox" /></th>
<th style="text-align: center; border: 1px solid #eaeaea">Email Address</th>
<th style="text-align: center; border: 1px solid #eaeaea">Select Option</th>
</tr>
</thead>
<tbody>
@foreach (var row in Model)
{
<tr>
<th scope="row"><input type="checkbox" class="checkBox" value="@row.site"></th>
<td class="gfgusername" style="width: 20%; padding: 0px; text-align: center; border-left: 1px solid #eaeaea; border-right: 1px solid #eaeaea">
@row.EmailAddress.Trim()
</td>
<td style="width: 20%; padding: 0px; text-align: center; border-right: 1px solid #eaeaea">
<select class="priorityList" name="priorityList2"><option>Yes</option>
<option>No</option><option>Delete Folder</option></select>
</td>
</tr> }
</tbody>
</table>
<script language="javascript">
$(document).ready(function () {
$("#delete").click(function () {
$('input:checkbox.checkBox').each(function () {
if ($(this).prop('checked')) {
???????????
});
var options = {};
options.url = "/Dashboard/Delete";
options.type = "POST";
options.data = ????;
options.contentType = "application/json";
options.dataType = "json";
options.success = function (msg) {
alert(msg);
};
options.error = function () {
alert("Error while deleting the records!");
};
$.ajax(options);
});
});
</script>
我的問題是我們如何保存用戶回應并通過 AJAX 傳遞,如果用戶想洗掉,我只能傳遞一個值,但不確定如何通過 ajax 傳遞多個值(只有用戶選中的復選框)。
uj5u.com熱心網友回復:
您的洗掉功能將如下所示:
$(document).ready(function () {
$("#delete").click(function () {
var myCheckboxes = [];
$('input:checkbox.checkBox').each(function () {
if ($(this).prop('checked')) {
myCheckboxes.push($(this).attr("value"));
}
});
var mySelectedValue= $('#priorityList2').find(":selected").text();
var json = {
myCheckboxes : myCheckboxes,
mySelectedValue: mySelectedValue
};
var options = {};
options.url = "@Url.Action("Delete", "Dashboard")";
options.type = "POST";
options.data = {"json": JSON.stringify(json)};
options.contentType = "application/json";
options.dataType = "json";
options.success = function (msg) {
alert(msg);
};
options.error = function () {
alert("Error while deleting the records!");
};
$.ajax(options);
})
});
你的Controller方法將是:
using System.Web.Script.Serialization;
[HttpPost]
public JsonResult Delete(string json)
{
var serializer = new JavaScriptSerializer();
dynamic jsondata = serializer.Deserialize(json, typeof(object));
//Get your variables here from AJAX call
var checboxValues = jsondata["myCheckboxes"];
var mySelectedValue = jsondata["mySelectedValue"];
//Do your stuff
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/417993.html
標籤:
