简体   繁体   English

如何使用jQuery从HTML表获取复选框和文本框值

[英]How to get checkbox and textbox value from HTML table using jquery

I have a dynamically created HTML table with values- Partial View There are two TextBoxes for 'Quantity' and 'Remarks' and one checkbox 我有一个带有值的动态创建的HTML表-Partial View有两个“数量”和“备注”的文本框和一个复选框

  <table style="border:0;width:100%;padding:5px" class="table_style" id="mytable">
    <tbody>
             @for (int i = 0; i < dtServices.Rows.Count - 1; i++)
             {           
                    <tr>
                        <td style="text-align:left;vertical-align:top">                
                            @Html.HiddenFor(model => model.ServiceListDetails.UserXId, new { Id = "hduId", Value = userId })
                            @Html.HiddenFor(model => model.ServiceListDetails.SessionXId, new { Id = "hdsId", Value = HttpContext.Current.Session["UserId"] })     
                            @Html.HiddenFor(Model => Model.ServiceListDetails.Id, new { @Id = "hdServiceId",Value = @dtServices.Rows[i]["ServiceId"].ToString() })  </td>

                            @* <td style="text-align:left;vertical-align:top">@dtServices.Rows[i]["ServiceId"].ToString()</td> *@
                            <td style="text-align:left;vertical-align:top">@dtServices.Rows[i]["ServiceName"].ToString()</td>
                            <td style="text-align:left;vertical-align:top">@dtServices.Rows[i]["ServiceUnit"].ToString()</td>

                            <td style="text-align:left;vertical-align:top">@Html.TextBoxFor(Model => Model.ServiceListDetails.ServicelistQuatity, new { @Id = "txtQty_" + @dtServices.Rows[i]["ServiceId"].ToString(), @style = "width:120px" })</td>
                            <td style="text-align:left;vertical-align:top">@Html.TextBoxFor(Model => Model.ServiceListDetails.ServicelistRemarks, new { @Id = "txtRemark_" + @dtServices.Rows[i]["ServiceId"].ToString(), @style = "width:200px" })</td>
                            <td>@Html.CheckBox("assignids")</td>    
                            <td style="text-align:left;vertical-align:top">

                        </td>
                    </tr>
             }
     </tbody>
</table>

I would like to get the values of the the selected rows by getting the selected row and the corresponding values in the textboxes using jquery on click of button in View 我想通过在视图中单击按钮时使用jQuery获取所选行和文本框中的相应值来获取所选行的值

  <input type="button" id="btnPreview" name="Preview" value=" Preview " class="button"/>

I tried googling and came to this. 我尝试了谷歌搜索并来到了这里。 But still not working.The values are 'undefined' 但仍然不起作用。值是``未定义的''

    $("#btnPreview").click(function () {
     var strmsg = "Values = ";
     var rows = $('#mytable tbody >tr');
     var columns;
     for (var i = 0; i < rows.length; i++) {
         columns = $(rows[i]).find('td');
         for (var j = 0; j < columns.length; j++) {

             if ($(columns[j]).find('assignids').is(':checked'))
             {
                 var itemcode = $(columns[j]).find('txtQty_').val();
                 strmsg = strmsg + itemcode;
             }
         }
     }

Please anyone help me.. I have been stuck with this for more than a week now :-((( 请任何人帮助我..我已经坚持了一个多星期了:-(((

Any help appreciated. 任何帮助表示赞赏。

Instead of using $(rows[i]) and $(columns[j]) , have you tried using jQuery's eq() method instead? 而不是使用$(rows[i])$(columns[j]) ,您是否尝试过使用jQuery的eq()方法?

Here's a good resource for it: http://api.jquery.com/eq/ 这是一个很好的资源: http : //api.jquery.com/eq/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM