[英]access html elements using class name in JavaScript
有沒有辦法在JavaScript中訪問特定類名下的html元素?
她是我迄今為止所擁有的:
部分:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Student.Models.vwStudent>>" %>
<table>
<tr>
<th>
Student ID
</th>
<th>
Past Due Amount
</th>
<th>
Past Due Days
</th>
</tr>
<% if (Model != null)
foreach (var item in Model) { %>
<tr>
<td class="StudentInv">
<%=Html.TextBox("StudentID",item.StudentID) %>
</td>
<td class="StudentInv">
<%=Html.TextBox("PastDueAmount",item.PastDueAmount) %>
</td>
<td class="StudentInv">
<%=Html.TextBox("PastDueDays",item.PastDueDays) %>
</td>
</tr>
<% } %>
主:
<div>
<% using(Html.BeginForm("SaveStudentInvoice", "StudentInvoice")) %>
<% { %>
<div>
<% Html.RenderPartial("DisplayStudentInvoiceList"); %>
</div>
<% } %>
<br/>
<input type="button" id="Submit" name="Submit" value="Submit" />
</div>
JavaScript的:
$('#Submit').click(function () {
var link = '/StudentInvoice/SaveStudentInvoice';
$.ajax({
type: 'POST',
url: link,
data: { SaveStudent: $(".StudentInv").val()
},
dataType: 'json',
success: function (result) {
alert("Success")
},
error: function (result) {
alert("Failed")
}
});
});
控制器:
[HttpPost()]
public ActionResult SaveStudentInvoice(string SaveStudent)
{
/// Parse SaveStudent String and rerform some Action
return View();
}
你的jQuery
應該是
$(".StudentInv input")
選擇具有StudentInv
類的節點內的input
對象。
更新使用列表
請注意,因為您的查詢返回多個節點, $.val()
僅返回第一個項的值。 您可以通過http://jsfiddle.net/Qs4JC/2/遍歷每一個
var values = [];
$(".StudentInv input").each(function(){
values.push($(this).val());
});
// values is populated here
請使用Jquery,就像這個$('.StudentInv :input');
獎金: http : //jsfiddle.net/Qs4JC/1/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.