listener + jquery object + trigger change ebent + append
我想:
这一步#4无效
// add listener on change in options
$('.cl_preAction').on("change",function(){
alert("");
})'
// set up html string with options
glCfgSection = <select name='ACTC' class='cl_preAction' id='cl_preAction' data-theme='a'>\n\
<option data-location='T' value='001'>Option 1</option>\n\
<option data-location='T' value='002'>Option 2</option>\n\
<option data-location='T' value='003'>Option 3</option>\n\
<option data-location='T' value='004'>Option 4</option>\n\
</select>\n\";
// convert string to jquery object
myTmpl = $(glCfgSection);
// trigger a change event - neither of these seem to work
//$('.cl_preAction option', myTmpl).change();
$('.cl_preAction option', myTmpl).trigger('change');
// append to page (jQuery Mobile)
myTmpl.appendTo("#placeholder"+glCurrentTab).trigger('create'); // this works
答案 0 :(得分:2)
您必须进行2项更改。
尝试
// use event delegation to handle dynamic elements
$(document).on("change",'.cl_preAction', function(){
alert("");
});
// set up html string with options
glCfgSection = "<select name='ACTC' class='cl_preAction' id='preAction' data-theme='a'>\n\
<option data-location='T' value='001'>Option 1</option>\n\
<option data-location='T' value='002'>Option 2</option>\n\
<option data-location='T' value='003'>Option 3</option>\n\
<option data-location='T' value='004'>Option 4</option>\n\
</select>\n";
// convert string to jquery object
myTmpl = $(glCfgSection);
// append to page (jQuery Mobile)
//fire the event after adding the element to dom because the listener is added to the document object
myTmpl.appendTo("#placeholder"+glCurrentTab).trigger('change').trigger('create'); // this works (edited string delimiters)