How do you dynamically create a radio button in Javascript that works in all browsers?

后端 未结 11 1519
深忆病人
深忆病人 2020-12-01 12:12

Dynamically creating a radio button using eg

var radioInput = document.createElement(\'input\');
radioInput.setAttribute(\'type\', \'radio\');
radioInput.se         


        
11条回答
  •  抹茶落季
    2020-12-01 12:39

    Quick reply to an older post:

    The post above by Roundcrisis is fine, IF AND ONLY IF, you know the number of radio/checkbox controls that will be used before-hand. In some situations, addressed by this topic of 'dynamically creating radio buttons', the number of controls that will be needed by the user is not known. Further, I do not recommend 'skipping' the 'try-catch' error trapping, as this allows for ease of catching future browser implementations which may not comply with the current standards. Of these solutions, I recommend using the solution proposed by Patrick Wilkes in his reply to his own question.

    This is repeated here in an effort to avoid confusion:

    function createRadioElement( name, checked ) {
       var radioInput;
       try {
            var radioHtml = '

提交回复
热议问题