[英]How to handle multiple radio buttons inside multiple divs
我在容器中输入字段很少,用户可以将这些字段相乘。 我需要根据容器更改输入字段的名称。
<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', '...');
看起来您在选择器上遇到了一些问题。 像这样尝试:
//Loop through each .station
$(".stations").each(function(sIndex){
//Loop through each radio buttons inside the container
$(this).find("input:radio").each(function(rIndex){
// Change the radio name attribute
$(this).attr('name','pp[prc]['+sIndex+'][stations]['+rIndex+'][id]');
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.