我正在开发一个网站,该网站必须根据用户选择的值(在<select>
中)显示各种产品的特定表单 - 因此可以在循环中动态创建多个表单通过javascript函数(buildform()
)。该代码不起作用,例如表单不会创建/附加到包装上。我缩小了问题,我认为这个问题涉及到jQuery选择器/ div-id的不同值(#ecorpproductwrapper"+ecorp_eprodselectid)
。 当我使用(只是作为一个测试)#ecorpproductwrapper"
(没有变量ecorp_eprodselectid;
也见下面的代码在替代工程下)代码工作正常,例如,表格是建立的。我通过控制台检查,ecorpproductwrapper"+ecorp_eprodselectid
值是相同的div-id和jquery选择器,所以我不明白哪里出了问题?动态创建表格取决于<select>不起作用
请参见下面的简化代码:
for(var i=0;i<5;i==){
var ecorp_eprodselectid; //will have various values
//function to build form depending on selected value in <select class= eprodtype"+ecorp_eprodselectid >
$(".eprodtype"+ecorp_eprodselectid).focus(function() {
var previous;
// Store the current value on focus and on change
previous = this.value; //old select value
}).change(function() {
var optionsform = buildform(this.value);
console.log('append form'+optionsform);
//NEXT 2 lines doe NOT WORK
$("#ecorpproductwrapper"+ecorp_eprodselectid).children().remove(); //remove previous form
$("#ecorpproductwrapper"+ecorp_eprodselectid).append(optionsform);
//ALTERNATIVE works: $('#ecorpproductwrapper').children().remove(); //remove previous tariif struct form
//ALTERNATIVE works: $('#ecorpproductwrapper').append(optionsform);
var str = "#ecorpproductwrapper"+ecorp_eprodselectid;
console.log('STRING ECORP PRODUCT APPEND: '+str);
console.log('change eprod val: '+this.value);
previous = this.value;
});//$("").focus(function() {
}//for i
//function to build form
var buildform = function(ecorp_eproductid) {
//some code here
//NEXT LINE does not work:
form += '<td> <div id="ecorpproductwrapper'+ ecorp_eprodselectid+'"> </div> </td> </tr>'; //cell with wrapper for ecorp product info
//ALTERNATIVE WORKS: form += '<td> <div id="ecorpproductwrapper"> </div> </td> </tr>'; //cell with wrapper for ecorp product info
//some code here; returns form
}//function buildform
创建你的代码的小提琴 –