动态添加的单选按钮onclick事件不起作用? [英] dynamically added radio buttons onclick event not working?
问题描述
我正在尝试
1.从用户选择中使用Ajax获得响应,这将单选按钮属性列表作为
返回给我2.一旦获得属性列表,我就创建了收音机使用createElement()
按钮。3.然后将事件附加到onclick按钮的单选按钮。
4.然后,我使用pedChild
I am trying following, 1. Get response using Ajax from the users selection which returns me the list of the radio buttons attributes as 2. Once i get the attribute list i am creating the radio buttons using createElement() 3. Then i am attaching the event to the radio button for onclick buttons. 4. Then i add element to the DOM using appedChild
将元素添加到DOM中,下面是添加单选按钮(对于IE)的代码
The below is code for adding radio button (for IE)
var radio = '<input ';
radio += 'type ="radio" ';
radio += 'id="' + id + '" ';
radio += 'value="" ';
radio += '/>';
radioButton = document.createElement(radio);
radioButton.onClick = function(){alert('this is test')}
ele.appendChild(radioButton);
ele.innerHTML += 'none' + '<br/>';
现在,当我向DOM中添加单选按钮时,onclick不起作用,我不知道不明白为什么?
Now in all this when i am adding the radio button to the DOM onclick is not working and i don't understand why ?
谢谢
推荐答案
您可以动态地通过两种方式向DOM中添加en元素:通过将html代码插入父元素或通过创建具有某些属性的子元素。在您的示例中,这两种方法是混合的,因此毫无意义。
You can dynamically add en element to the DOM by two ways: by inserting an html code into the parent element or by creating a child element with some properties. In your example the two methods are mixed - therefore senseless.
方法1.插入HTML代码:
Method 1. Inserting HTML code:
var radio = '<input type ="radio" id="' + id + '" value="" />';
ele.innerHTML = radio;
document.getElementById(id).onclick = function(){ alert('this is test'); };
方法2。创建DOM子级:
Method 2. Creating DOM child:
var radioButton = document.createElement('input');
radioButton.type = "radio";
radioButton.id = id;
radioButton.onclick = function(){ alert('this is test'); };
ele.appendChild(radioButton);
这篇关于动态添加的单选按钮onclick事件不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!