选中父 div 时检查单选按钮?

Wil*_*tle 0 html jquery

我有一个单选按钮,如果用户单击其内部的 div,我希望可以选择该按钮。我有一个 div 的点击事件,但我似乎无法检查单选按钮。

继承人我的代码:

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");
});
Run Code Online (Sandbox Code Playgroud)

Aru*_*hny 5

尝试.prop()而不是.attr()

$( "#controlDiv" ).click(function() {
    var radButton = $(this).find('input[type=radio]');
    $(radButton).prop("checked", true);
});
Run Code Online (Sandbox Code Playgroud)