动态创建的表单取决于<select>不起作用</select>

时间:2013-11-14 11:42:05

标签: javascript jquery

我正在开发一个网站,必须根据用户选择的值(<select>)显示各种产品的特定表单 - 所以通过javascript在循环中动态创建各种表单的数字功能(buildform())。代码不起作用,例如表单不会创建/附加到包装器。我缩小了问题,我认为问题与jquery selectors / div-id的(#ecorpproductwrapper"+ecorp_eprodselectid)的不同值有关。 当我使用(仅作为测试)#ecorpproductwrapper"(没有变量ecorp_eprodselectid;时也参见下面的代码ALTERNATIVE WORKS中)代码工作正常,例如表格是建立的。我通过控制台检查了div {id和jquery选择器的ecorpproductwrapper"+ecorp_eprodselectid值是否相同,所以我不明白出了什么问题?

请参阅下面的简化代码:

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

2 个答案:

答案 0 :(得分:2)

我想你忘了在你的函数中添加ecorp_eprodselectid。

var buildform = function(ecorp_eprodselectid ) {

答案 1 :(得分:0)

我们对上面给出的文字几乎没有假设:

  • 你知道这个。值得工作
  • console.log显示optionsform包含它应具有的HTML。在OP中没有说,但如果没有,该功能不起作用。如buysDB
  • 所述,函数似乎已经遗失var buildform = function(someVar)

由于我看不到你的代码,我会首先尝试通过chaned清除所有内容:

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

为:

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

然后:

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

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

如果您在(#ecorpproductwrapper“+ ecorp_eprodselectid)中也有文本使用children(),请考虑选择可以清除的DIV。

如果仍然无法解决问题,则需要考虑一些问题。

相关问题