0

The code below relates to a simplified form with a number of repetitive sets of form fields. I'm writing some javascript to insert values in the form fields and then display the form fields, but struggle to get the right syntax for the jquery selectors - e.g., to use variables with the selectors. I looked at http://api.jquery.com/html/ and http://api.jquery.com/remove/ and some code examples like at http://jsfiddle.net/LYDuZ/, but still get stuck. Pls your help with respect to the right code for the javascript html() and remove() code.

Thank you in advance..

html code:

<style>
  .hideElement {display:none;}
</style>


<form>
  <!-- first set-->
  <div id="product0" class="hideElement"> Product <br>
  <p> productname: </p>  <div id="fld_name0"> </div>

  <!-- second set-->
  <div id="product1" class="hideElement"> Product <br>
  <p> productname: </p>  <div id="fld_name1"> </div>

</form>

javascript code:

for(var i=0;i<msg.nrofproducts;i++){

  $("#fld_name'+i'").html(msg.productname[i]);      

  $("#product'+i'").remove('.hideElement'); //finally show product
}//for i
4

4 回答 4

2

只需正确连接您的 ID:

$("#fld_name" +i).html(msg.productname[i]);      

$("#product" +i).remove('.hideElement'); //finally show product
于 2013-08-06T13:02:35.410 回答
0
  1. 正确连接,即:"#fld_name" + i
  2. 看来你想删除一个类,所以使用removeClass而不是remove

像这样:

for(var i=0;i<msg.nrofproducts;i++){

    $("#fld_name" + i).html(msg.productname[i]);      

    $("#product" + i).removeClass('hideElement'); //finally show product
}
于 2013-08-06T13:07:32.720 回答
0

尝试连接

$('#product'+i).remove(...);
于 2013-08-06T13:04:25.003 回答
0

不确定您的需求,但条件存在问题。应该 :

 if( x== 1 )

小提琴

于 2013-08-06T13:10:16.847 回答