如何在动态创建的下拉列表上调用onchange函数。代码如下: HTML代码:
<img id="addColumn" src="images/icons/panel_add.gif" alt="Add Column" onclick="addColumnDynamically('q_123_456_789')"/>
<div class = "q_123_456_789"></div>
的Javascript
function addColumnDynamically(currentObjId){
maxRepeatedColumnSeq = maxRepeatedColumnSeq+1;
var dropDownIdStr = currentObjId;
dropDownIdStr = dropDownIdStr.substring(dropDownIdStr.indexOf("_") + 1);;
dropDownIdStr = "q_drop_"+dropDownIdStr+"_"+maxRepeatedColumnSeq;
alert("idStr........"+dropDownIdStr);
$("."+currentObjId).append('<div class="Content22">\
<br>\
<input class="input numberElement" type="text" style="width: 25%" id='+currentObjId+"_"+maxRepeatedColumnSeq+'>\
<select class="selectStyle1" style="width: 25%" id='+dropDownIdStr+'<option value=""></option><option value=""></option><option value="Hours">Hours</option><option value="Days">Days</option><option value="Weeks">Weeks</option><option value="Months">Months</option><option value="Years">Years</option> >\
<img src="images/icons/panel_remove.gif" onclick="removeColumn(this)">\
<br>\
</div>');
$('#'+currentObjId+"_"+maxRepeatedColumnSeq).on('blur', function () {
alert("Hiii....");
});
alert("Hiii.."+dropDownIdStr);
$('#'+dropDownIdStr).on('change', function () {
alert("getElementById...."+document.getElementById(dropDownIdStr));
});
}
onblur函数正在调用,但是onchange没有调用。我怎么能这样做?
答案 0 :(得分:0)
在您的代码中更改onchange以进行更改!此外,因为动态创建元素最好使用:
$(document).on(event, element, handler)
在你的情况下:
$(document).on('change', '#'+dropDownIdStr, function () {
alert("getElementById...."+document.getElementById(dropDownIdStr));
});
答案 1 :(得分:0)
事件是“改变”,而不是“改变”#。
$('#' + dropDownIdStr).on('change', function () {
alert("getElementById...." + document.getElementById(dropDownIdStr));
});
答案 2 :(得分:0)
看看代码:你错过了结束“&gt;”和结束"</select>"
$(document).ready(function(){
$(document).on('click', '#addColumn', function(evt){ addColumnDynamically('q_123_456_789')} );
})
function addColumnDynamically(currentObjId){
var maxRepeatedColumnSeq = maxRepeatedColumnSeq+1;
var dropDownIdStr = currentObjId;
dropDownIdStr = dropDownIdStr.substring(dropDownIdStr.indexOf("_") + 1);;
dropDownIdStr = "q_drop_"+dropDownIdStr+"_"+maxRepeatedColumnSeq;
alert("idStr........"+dropDownIdStr);
$("."+currentObjId).append('<div class="Content22">\
<br>\
<input class="input numberElement" type="text" style="width: 25%" id='+currentObjId+"_"+maxRepeatedColumnSeq+'>\
<select class="selectStyle1" style="width: 25%" id='+dropDownIdStr+'><option value=""></option><option value=""></option><option value="Hours">Hours</option><option value="Days">Days</option><option value="Weeks">Weeks</option><option value="Months">Months</option><option value="Years">Years</option></select>\
<img src="images/icons/panel_remove.gif" onclick="removeColumn(this)">\
<br>\
</div>');
alert("Hiii.."+dropDownIdStr);
$('#'+dropDownIdStr).on('change', function () {
alert("getElementById...."+document.getElementById(dropDownIdStr));
});
}