0

Question: How to dynamically add radio buttons to a form

I am appending the radio buttons:

function addTypesButtons()
{
var params=[];
params.table='TB_TYPES_ADDRESS';
params.field='TYPES_ADDRESS_NAME';

$.getJSON("php/CoJ_getTypesNames.php", $.param(params, true) , function(data) {
  $.each(data, function(index, element)
  {
      if (index==0)
      {
         $('#aTypes').append($('<input>', {
             type:"radio", class:"toggle", disabled:true, name:"addresstypes", onclick:"AddressTypeClick("+index+1+")", 
                    tabindex:1, id:"addresstypeshome", checked:true, value:element[params.field]})+element[params.field]+'<br/>');
      }
     else 
     { 
         $('#aTypes').append($('<input>', {
             type:"radio", class:"toggle", disabled:true, name:"addresstypes", onclick:"AddressTypeClick("+index+1+")", 
                    tabindex:1, id:"addresstypeswork", value:element[params.field]})+element[params.field]);
     }
    } 
); });          
}

I expect to get two radio buttons like this: Home
Work

What I really get is what the top of the image shows:

This should show on the form like the bottom of the image:

Types http://www.greatdaydan.com/Types.png

How do I get just the text without the " [object Object]"?

4

1 回答 1

0

添加 .attr 并正确关闭括号

  var radioBtn = $('<input>').attr({
        type:"radio", class:"toggle", disabled:true, name:"addresstypes", onclick:"AddressTypeClick("+index+1+")",tabindex:1, id:"addresstypeshome",
value:"element[params.field]"+element[params.field]});
$('#aTypes').append(radioBtn);
if (index==0) $('#aTypes').append("<br/>");

或者,你可以做这样的事情(显然添加更多你想要的属性到 radioBtn)

var radioBtn = $('<input type="radio" id="addresstypeshome" name="addresstypes" />');

radioBtn.appendTo('#aTypes');
于 2013-09-06T15:29:37.017 回答