使用jQuery中的类访问文本框(HTMLHelper)值

时间:2012-07-23 20:46:56

标签: javascript jquery asp.net-mvc partial-views html-helper

如何在JavaScript函数(jQuery)中访问“StudentID”值。

HTML:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Student.Models.vwStudent>>" %>

    <div class="divClass">
      <table class="tableClass">
      <% foreach (var item in Model) { %>
        <tr class="trClass">
          <td class="tdClass">
            <%= Html.TextBox("StudentID") %>
          </td>
        </tr>
      <% } %>
      </table>
    </div>

JQuery的:

 $('#ShowStudentID').click(function () {        
      $(".tdClass").each(function () {
          alert($(this).val());           
      });
 });

硕士(表格):

<% using(Html.BeginForm("SaveCommitment", "LPForm")) %>
<% { %>
<div>
<% Html.RenderPartial("DisplayStudents"); %>
</div>
<% } %>
<br/><br/>
<input type="button" id="ShowStudentID" name="ShowStudentID" value="Show StudentID" />
</div>

我尝试过使用text(),innerHTML()和innerText()方法但是无法使用它。任何帮助将不胜感激。

提前致谢

2 个答案:

答案 0 :(得分:1)

如果要获取.tdClass中输入的值,可以尝试以下操作:

 $('#ShowStudentID').click(function () {        
      $(".tdClass").find('input').each(function() {
          alert($(this).val());           
      });
 });

答案 1 :(得分:0)

通过ID轻松快捷地访问它:

$('#StudentID').val();