2013-11-14 26 views
0

我正在开发一个网站,该网站必须根据用户选择的值(在<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 
+3

创建你的代码的小提琴 –

回答

2

我想你忘了你的函数添加ecorp_eprodselectid。上述

var buildform = function(ecorp_eprodselectid) { 
+0

你说的函数参数丢失是正确的,但这是代码复制到这个网站时的类型错误,例如,它不是原因,无论如何thnx – Joppo

0

几件事情我们就假设给定的文本:

  • 你知道THIS.VALUE工作
  • 的console.log显示optionsform有HTML,它应该有。在OP中没有说,但如果没有,该功能不起作用。功能似乎由buysDB

注意到当我看不到你的代码中已经丢失var buildform = function(someVar),我会尝试先清一切100%的chaning此:

$("#ecorpproductwrapper"+ecorp_eprodselectid).children().remove(); 

到:

$("#ecorpproductwrapper"+ecorp_eprodselectid).html(""); 

Then:

$("#ecorpproductwrapper"+ecorp_eprodselectid).html(optionsform); 

如果您无意在DIV中保留任何内容,则无需追加。

如果你还在(#ecorpproductwrapper“+ ecorp_eprodselectid),这就是为什么你使用儿童()文本,考虑选择那些可以清除的DIV。

如果仍然不行,东西冷落

+0

thnx for你的评论和关于html()的提示我刚刚发现ecorp_eprodselectid变量不包含唯一值,因此一些div使用相同的id创建。我必须弄清楚如何改变这个代码,但我认为这是个问题...... – Joppo