【问题标题】:Get the ID in row that has radio button checked [duplicate]获取选中单选按钮的行中的 ID [重复]
【发布时间】:2022-02-05 00:47:28
【问题描述】:

我有这个函数,它在模式弹出窗口中创建一个表,并且该表是从从 ajax 调用传入的数组中的数据填充的。现在,单击模式弹出窗口中的按钮,我需要获取 item.TimedPathwayID 的值,该值已选中其单选按钮并将其添加到隐藏字段。

function PopulateTimedPathwaysTable(tblData) {
var tbody = $('#tblTimedPathways tbody');
$.map(tblData.d, function (item) {
    tr = $('<tr></tr>');
    tr.append('<td class="pathwayID">' + item.TimedPathwayID + '</td>');
    tr.append('<td>' + item.TimedPathwayName + '</td>');
    tr.append('<td><input type="radio" class="radioSelection" name="timedPathwaySelection"" />');
    tbody.append(tr);
});
$('input[name=timedPathwaySelection]:first').attr('checked', true);}
}

我一直在摆弄这种事情,但没有任何乐趣,并且默认情况下会检查第一行中的单选按钮,因此如果用户只接受默认值而不点击,则它不能真正与点击事件相关联.那请问我该怎么做呢?

$('body').on('click', '.radioSelection', function () {
    var $tbl = $('#tblTimedPathways tbody');
    var $dataRow = $tbl.closest('tr');
    var id = $dataRow.find('td').eq(0).html(); 
});

【问题讨论】:

  • @freedomn-m 好的,谢谢,但是如果没有点击事件并且只需要选中默认按钮的行中的 id 怎么办?
  • 没有点击事件(所以没有this),您可以使用$("[name=timedPathwaySelection]:checked") 获取当前收音机,例如:jsfiddle.net/xnr0om9k
  • @freedomn-m 不确定这是否有效?
  • 那么您需要将无线电选择器与 .closest(tr) 和 .find(.pathwayID).text() 结合起来。您还需要确保在正确的时间调用它,即在 tbody.append 之后。
  • 请注意,当你想返回一个数组时,你应该只使用$.map——否则你应该使用.each。没有真正的原因,这就是$.each/$.map 的用途。

标签: javascript jquery asp.net onclick radio-button


【解决方案1】:
  • .closest 上升到 html 树,所以 tbody.closest(tr) 不太可能是你想要的。
  • 然后您需要返回到包含所需数据的单元格。
 let $this = $(this);  //this is the radio button
 let id = $this.closest("tr").find("td.pathwayID").text();

我还要回应说,我通常会将id 作为属性添加到行中,以消除以后查找的必要性。

//sample data
let data = {
  d: [{
      TimedPathwayID: 1,
      TimedPathwayName: "test"
    },
    {
      TimedPathwayID: 2,
      TimedPathwayName: "test"
    },
    {
      TimedPathwayID: 3,
      TimedPathwayName: "test"
    }
  ]
};

function PopulateTimedPathwaysTable(tblData) {
  var tbody = $('#tblTimedPathways tbody');
  $.map(tblData.d, function(item) {
    tr = $('<tr></tr>');
    tr.append('<td class="pathwayID">' + item.TimedPathwayID + '</td>');
    tr.append('<td>' + item.TimedPathwayName + '</td>');
    tr.append('<td><input type="radio" class="radioSelection" name="timedPathwaySelection"" />');
    tbody.append(tr);
  });
  $('input[name=timedPathwaySelection]:first').attr('checked', true);
}
//populate it
PopulateTimedPathwaysTable(data);

$('body').on('click', '.radioSelection', function () {
    let $this = $(this);  //this is the radio button
    //console.log($this);
    let id = $this.closest("tr").find("td.pathwayID").text();
    console.log(id);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="tblTimedPathways">
  <tbody>
  </tbody>
</table>

【讨论】:

  • 抱歉,添加 cmets 并不理想。当我没有时间给出完整答案时,我倾向于这样做,但这只是一个糟糕的借口。
  • 不怪,现在我觉得回答基本上和你一模一样。我也倾向于快速得到答案,然后再详细说明。这也不理想。
  • 如果你们都可以在评论或回答之前找到重复的,我们都会更好:-P。
  • @Steve0 同样的问题是针对freedomn-m,但是如果没有单选按钮点击事件,但是在表格所在的模式弹出窗口中有点击怎么办?
  • @HereticMonkey 没有给出完整答案的另一个原因 - 总是重复 :)
猜你喜欢
  • 2016-07-13
  • 1970-01-01
  • 1970-01-01
  • 2019-08-23
  • 2015-09-09
  • 1970-01-01
  • 2017-12-20
  • 1970-01-01
  • 2021-05-02
相关资源
最近更新 更多