简体   繁体   English

如何使用 jQuery 获取选中的行值?

[英]How to get checked row values with jQuery?

I have been trying to get a value of a hidden textbox which are model bind using razor.我一直在尝试获取使用剃刀进行模型绑定的隐藏文本框的值。 The table consists of 2 hidden input types for each row.该表由每行的 2 个隐藏输入类型组成。 I want to get value of the textbox where the id says purDate .我想获取ID 为purDate的文本框的值。

How do I do this.我该怎么做呢。

Here's the table for references这是参考表

<table id="accounts" class="ui celled selectable table">
<thead>
 <tr>
 <th>Acc No</th>
 <th>Purchased Date</th>
 </tr>
 </thead>
 <tbody>
 @foreach (var item in Model.WithdrawelAccounts)
 {
  <tr>
  <td style="text-align:center">
   <input id="@item.ACC_AC_ID" type="checkbox" class="ui checkbox check" name="example" />
   <input id="accts" type="hidden" value="@item.ACC_NO_OF_AC_CURRENT" style="display:none" />
   </td>
    <td style="text-align:center">
        @item.ACC_PURCHASE_DATE.ToString("dd/MMM/yyy")
        <input class="pDate" id="purDate" type="hidden" name="purDate" value="@item.ACC_PURCHASE_DATE" style="display:none" />
    </td>
 </tr>
 }

 </tbody
</table>

Here's the they way I tried to retrieve the values这是我尝试检索值的方式

<script>

$('#frm_submit').click(function (event) {

var purDate = [];

 $.each($("input[type=checkbox]:checked").parents("td"), function () {
            noOfAcc.push($(this).find('input[type=hidden]').attr("value"));
            purDate.push($('#purDate').val());
            //purDate.push($(this).find("input[id='purDate']").text());
        });
});
 var purDate = [];
</script>

As you can see I have tried to get the value using id purDate but the result have duplicate values如您所见,我尝试使用 id purDate获取值,但结果具有重复值

The output on console.控制台上的输出。

purdate (2) ["2/1/2014 12:00:00 AM", "2/1/2014 12:00:00 AM"]

As you can see it has taken only the first <td> value in the loop according to what I think.正如您所看到的,根据我的想法,它只采用了循环中的第一个<td>值。

The actual result should be实际结果应该是

purdate (2) ["2/1/2014 12:00:00 AM", "2/4/2019 12:00:00 AM"]

How to I correctly loop and get the values in the table according to my need.如何根据需要正确循环并获取表中的值。 Help would be appreciated.帮助将不胜感激。

你能试试这个吗

document.formName.elements['purDate'].value

You are having an error in here.你在这里有错误。

$('#purDate').val()

It gets the first #purDate value.它获取第一个#purDate值。 Your id should be unique.您的id应该是唯一的。 To fix this, you can put this in a class.为了解决这个问题,你可以把它放在一个类中。

Remove the duplicate id and move it to class .删除重复的id并将其移动到class

@foreach (var item in Model.WithdrawelAccounts)
 {
  <tr>
  <td style="text-align:center">
   <input id="@item.ACC_AC_ID" type="checkbox" class="ui checkbox check" name="example" />
   <!-- Remove the accts id-->
   <input class="accts" type="hidden" value="@item.ACC_NO_OF_AC_CURRENT" style="display:none" />
   </td>
    <td style="text-align:center">
        @item.ACC_PURCHASE_DATE.ToString("dd/MMM/yyy")
        <!-- Remove the purDate id and put in in a class-->
        <input class="pDate purDate" type="hidden" name="purDate" value="@item.ACC_PURCHASE_DATE" style="display:none" />
    </td>
 </tr>
 }

Then in your jQuery:然后在你的 jQuery 中:

$.each($("input[type=checkbox]:checked").parents("td"), function () {

    //noOfAcc.push($(this).find('input[type=hidden]').attr("value"));
    //purDate.push($('#purDate').val());

    // This will get the closest class in selected checkbox
    noOfAcc.push($(this).closest('.accts').val());
    purDate.push($(this).closest('tr').find('.pDate').val());

    //purDate.push($(this).find("input[id='purDate']").text());
});

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

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