1

I am looking for a solution for following:

For JQuery: How can I change the attribute of all parents of objects that have the same name?

Here is my code:

<div class="roundedOne">
  <input type="radio" id="id_single" name = "radStatus" value = "1" />
  <label for="id_single"></label>
</div>          

<div class="roundedOne">
  <input type="radio" id="id_married" name = "radStatus" value = "1" />
  <label for="id_single"></label>
</div>  

I want to do something like : $("[name='radStatus']").parent().attr("class", "myNewClassName");

Thanks :)

4

3 回答 3

2

尝试这个

 $(function(){
    $("[name='radStatus']").parent().attr("class", "myNewClassName");
 });

演示

您的代码正在运行。但你需要,如何实施。看演示

于 2013-06-12T04:24:15.487 回答
1

您可以使用.parents() jQuery 方法。

所以这:

$("[name='radStatus']").parent().attr("class", "myNewClassName");

变成这样:

$("[name='radStatus']").parents().attr("class", "myNewClassName");

这是一个有效的小提琴,它将改变元素的所有父母。检查元素以查看新的类名。

于 2013-06-12T04:26:47.030 回答
1

尝试

$("input[name ='radStatus']").parent().addClass("myNewClassName");

于 2013-06-12T04:22:34.423 回答