简体   繁体   English

在动态创建的单选按钮上设置值或名称属性时出现问题

[英]Problem setting the value or name attribute on a dynamically created radio button

I am trying to dynamically generate a group of radio button, however when I add a radiobutton with jQuery the name attribute is not set properly. 我试图动态生成一组单选按钮,但是当我使用jQuery添加单选按钮时,name属性设置不正确。

        var radioButtonInput = document.createElement("input")
        var groupId = groupNodes[i].getAttribute("id");
        var groupName = groupNodes[i].getAttribute("displayName");

        $(radioButtonInput).attr("type","radio");
        $(radioButtonInput).attr("name","radioGroup");
        $(radioButtonInput).attr("id", groupId);

        $("#meetingType h2").after(radioButtonInput);

The radio buttons are created correctly but the name attribute is not present. 单选按钮已正确创建,但name属性不存在。 I've tried to use the html dom attribute .name but it generates the same result. 我尝试使用html dom属性.name,但它会产生相同的结果。

Try this : 尝试这个 :

var groupId = groupNodes[i].getAttribute("id");
var groupName = groupNodes[i].getAttribute("displayName");
var radioButtonInput = $("<input>", { "type" : "radio", "id" : groupId, "name" : "radioGroup"});

$("#meetingType h2").after(radioButtonInput);

where do you use groupName because here the input name will be "radioGroup" and not groupName 您在哪里使用groupName,因为这里的输入名称将是“ radioGroup”,而不是groupName

I would go for the most straightforward way: 我会采用最直接的方式:

$("#meetingType h2").after('<input type="radio" id="' + groupNodes[i].id + '" name="' + groupNodes[i].getAttribute("displayName") + '" />');

If still no luck please elaborate on "name attribute is not present" - how can you tell that? 如果仍然不走运,请详细说明“名称属性不存在”-您如何分辨呢? How do you check? 你如何检查?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM