用JS获取table每一行的数据Text或者input标签都可以
这篇文章是因为我当时想将table的值全部一次获取传输到后台,一开始使用的方法是可以获取到数据的,但是并不能够实现一起传到后台,通过寻找找到了在视图创建list的方法但是并不能够进行赋值,最后通过一下的递归方式进行实现。
<table class="table table-bordered" id="GC" style="min-width:1000px;">
<thead style="background-color:#ebf0f5;">
<tr>
<th>Guarantor BP No.</th>
<th>Guarantor Name-EN</th>
<th>Guarantor Rating</th>
<th>Guarantor CBASE No.</th>
<th>Collateral Type</th>
<th>Guarantor New Rating</th>
<th>Guarantor New Rating Date</th>
<th>Guarantor New Rating Method</th>
<th>Guarantor Next Review Date</th>
</tr>
</thead>
<tbody>
@foreach (var item in ViewBag.Guarantor)
{
<tr>
<td><span id="GC_BP_No" name="GC_BP_No">@item.GC_BP_No</span></td>
<td><span id="GC_Name_EN" name="GC_Name_EN">@item.GC_Name_EN</span></td>
<td><span id="GC_Rating" name="GC_Rating">@item.GC_Rating</span></td>
<td><span id="GC_CBASE_No" name="GC_CBASE_No">@item.GC_CBASE_No</span></td>
<td><span id="Collateral_Type" name="Collateral_Type">@item.Collateral_Type</span></td>
<td><input type="text" class="form-control" style="height:100%;width:100%;" onblur="GCReviewData()" name="NG_Rating" id="NG_Rating" value="@item.NG_Rating" /></td>
<td><input type="text" class="form-control" style="height:100%;width:100%;" name="New_Rating_Date" id="New_Rating_Date" value="@item.New_Rating_Date" /></td>
<td><input type="text" class="form-control" style="height:100%;width:100%;" name="NG_Rating_method" id="NG_Rating_method" value="@item.NG_Rating_method" /></td>
<td><input type="text" class="form-control" style="height:100%;width:100%;" name="New_next_review_date" id="New_next_review_date" value="@item.New_next_review_date" /></td>
</tr>
}
</tbody>
</table>
function Guarantor(count) {
/*循环出表中所有的数据*/
var html = {
GC_BP_No,
GC_Name_EN,
GC_Rating,
GC_CBASE_No,
Collateral_Type,
NG_Rating,
New_Rating_Date,
NG_Rating_method,
New_next_review_date
};
for (var j = 0; j < 9; j++) {
var valueTd = "";
if (j>4) {
valueTd = document.getElementById("GC").rows[count].cells[j].children[0].value;
} else {
valueTd = document.getElementById("GC").rows[count].cells[j].childNodes[0].innerText;
}
if (j + 1 == 1) {
html.GC_BP_No = valueTd;
} else if (j + 1 == 2) {
html.GC_Name_EN = valueTd;
} else if (j + 1 == 3) {
html.GC_Rating = valueTd;
} else if (j + 1 == 4) {
html.GC_CBASE_No = valueTd;
} else if (j + 1 == 5) {
html.Collateral_Type = valueTd;
} else if (j + 1 == 6) {
html.NG_Rating = valueTd;
} else if (j + 1 == 7) {
html.New_Rating_Date = valueTd;
} else if (j + 1 == 8) {
html.NG_Rating_method = valueTd;
} else if (j + 1 == 9) {
html.New_next_review_date = valueTd;
}
html.Customer_Number = $("#Customer_Number").val();
}
var gc = @ViewBag.Guarantor.Count;
///*调用修改方法*/
$.ajax({
url: "/RatingChange/GuarantorSave",
type: "post",
async: false,
data: html,
dataType: "json",
success: function (data) {
if (data > 0) {
count = count + 1;
if (gc > 1 && count <= gc) {
Guarantor(count);
} else {
alert('保存成功');
}
} else {
alert('保存失败');
}
}
})
}
用JS去除Table里面每一行的值并用ajax传到后台使用,一开始想在视图中使用list 但是并没有找到方法,用C#创建的list也没有赋值成功,最后改为递归调用,每次传到后台一行数据进行更改,回调之后每次进行判断是否是最后一行的数据,若不是的话就调用自身Count+1继续执行。