javascript - 如何处理多个div内的多个单选按钮

标签 javascript jquery

我的容器中只有几个输入字段,用户可以将这些字段相乘。我需要根据容器更改输入字段的名称。

<div class="stations">
 <input type="radio" name="pp[prc][0][station][0][id]">
 <input type="radio" name="pp[prc][0][station][1][id]">
</div>

这是我的 HTML 表单。

$(".stations").each(function(sIndex){
//Loop thru all .station
  $("input:radio", $(this)).each(function(rIndex){
    //Loop thru all radio buttons inside that container and change their names accordingly
   $("input:radio", $(this)).attr('name','pp[prc]['+sIndex+'][stations]['+rIndex+'][id]');
  });
});

当我这样做时,由于某种原因,当用户重复<div class="station">时其内容单选按钮的名称不会相应更改。我哪里做错了?

最佳答案

您正在使用 $(selector, context) 并且输入元素不能有子元素,您应该编码:

$(this).attr('name', '...');

关于javascript - 如何处理多个div内的多个单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16005971/

相关文章:

javascript - jquery datepicker 不处理创建的元素

javascript - Chart.js : How to fill the area under the line to a certain point?

php - 如何将 PHP 返回的输出插入到 HTML <div> 中? (我正在使用 WordPress)

javascript - 如何在 ngDialog 中加载数据

javascript - 使用 JQuery 的响应式设计

javascript - 如何根据 <a> 点击设置选择值

javascript - 来自 GitHub 页面的 API 请求不起作用 ("mixed block")

JavaScript :TypeError: document. getElementById(...) 为空

jquery - 在 JQuery 数据表上使用 JEditable 时如何阻止行高更改

php - 如果 MYSQL 列值为 true,请选中复选框