我有很多按钮。如果我被他们的一个点击调用这个函数:如何从单选按钮中为元素设置属性而不提交
function addOptions(id)
{
var button = document.getElementById(id); //clicked button
radioDiv = document.querySelector("div.radioDiv");
radioDiv.style.visibility = "visible";
var raz = document.getElementsByName('status'); //get radioButtons
$(".radioDiv").ready(function() {
$('input[type=radio][name=status]').change(function(){
/*Here I'm trying to set attribute to only one button but
here the problem: when I click a few buttons (example: 1st then 2nd, then 3rd)
and on 3rd button I choice the "radioButton" this code is set Attribute for
all of them (3) */
button.setAttribute("status", this.value);
});
});
}
这里的index.html。 RadioDiv默认隐藏
<div class="layer1">
<div class="radioDiv">
<input type="radio" value="sale" name="status">111
<input type="radio" value="inverted" name="status">222
</div>
</div>
<div class="layer2"></div>
所以,我需要从RadioButton的值设置按钮的属性。
你能发布完整的代码?按钮也是'html'? –
按钮代码正在动态生成。这里是完整的html:'button class =“class1”id =“1.1”onclick =“addOptions('1.1')' –
@Teemu它的工作方式没有'$(”。radioDiv“)。 ...})'just'$('input [type = radio] [name = status]')。change(function(){..}' –