我有很多按鈕。如果我被他們的一個點擊調用這個函數:如何從單選按鈕中爲元素設置屬性而不提交
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(){..}' –