У меня есть переключатель, что я хотел бы быть выбранным, если пользователь нажимает на div, внутри которого он находится. У меня есть событие click для div, но я не могу заставить его переключаться.check radio button Выбор родительского div?
Heres мой код:
var new_div = document.createElement('div');
new_div.setAttribute('id', 'controlDiv');
new_div.setAttribute('class', 'ansClass');
var newAnswer = document.createElement('input');
newAnswer.setAttribute('type', 'radio');
newAnswer.setAttribute('name', 'choice');
newAnswer.setAttribute('class', 'radioButton');
$(newAnswer).attr("id", "button"+j);
var newLabel = $("<label>", {id: "label"+j, class: "ansLabel", text: "answer"+j});
$(new_div).append(newAnswer);
$(new_div).append(newLabel);
$("#controlDiv").click(function() {
var radButton = $(this).find('input[type=radio]');
radButton.click();
// $(radButton).attr("checked", "checked");
});
Попробуйте следующее: http://stackoverflow.com/questions/9525128/jquery-set-radio-button –