我有一个循环,当它被调用时,创建一个带有一些表单元素的div。每个div都基于一个变量,“i”赋予字段和div唯一的名称。有没有一种方法可以将变量存储在创建div的位置?在循环中获取变量的值
例如,创建了div1,并且其中的所有内容都具有附加到名称的1(变量)。表单元素相互依赖,并由ID调用。问题是,当我创建一个新的div并将变量(i)更改为2时,第一组表单元素尝试使用2而不是1.
有意义吗?
编辑:这是一些代码。这很麻烦,所以我提前道歉。
var i = 0;
$('a#add-product').click(function(event){
i++;
$('<div />').addClass('product').attr('id', 'product'+i)
.append($('<h2><img src="<?php echo base_url();?>img/product.png" alt="" />Product '+i+'</h2>'))
.append($('<div class="info-line"><label>Division</label><p><select id="selection-'+i+'" class="selection"><option value="">- Select a Division -</option><option value="abrasives">Abrasives</option><option value="tapes">Bonding, Surface Protection & Tapes</option><option value="packaging">Packaging</option></select></p></div>'))
.append($('<div class="info-line"><label>Category</label><p><select id="selectionresult-'+i+'" name="selectionresult-'+i+'" class="selectionresult"></select><span id="result-'+i+'" class="result"> </span></p></div>'))
.append($('<div class="info-line"><label>Product</label><p><select id="selectionresult2-'+i+'" name="selectionresult2-'+i+'" class="selectionresult2"></select><span id="result2-'+i+'" class="result2"> </span></p></div>'))
.append($('<a class="remove" href="#add-product" id="remove-product'+i+'"><img src="<?php echo base_url();?>img/remove-product.jpg" alt="" />Remove Product</a>'))
.appendTo("#products");
// START OF ADDITIONAL PRODUCT DROP DOWNS
$("#selectionresult-"+i).hide();
$("#selectionresult2-"+i).hide();
$("#selection-"+i).change(function() {
$(this).next(".selectionresult").hide();
$(this).next(".selectionresult2").hide();
$("#result-"+i).html('Retrieving ...');
$.ajax({
type: "POST",
data: "data=" + $(this).val(),
url: "<?php echo base_url();?>dropdown.php",
success: function(msg){
if (msg != ''){
$("#selectionresult-"+i).html(msg).show();
$("#result-"+i).html('');
}
else{
$("#result-"+i).html('<em>No item result</em>');
}
}
});
});
$("#selectionresult-"+i).change(function() {
$(this).next(".selectionresult2").hide();
$("#result2-"+i).html('Retrieving ...');
$.ajax({
type: "POST",
data: "data=" + $(this).val(),
url: "<?php echo base_url();?>dropdown.php",
success: function(msg){
if (msg != ''){
$("#selectionresult2-"+i).html(msg).show();
$("#result2-"+i).html('');
}
else{
$("#result2-"+i).html('<em>No item result</em>');
}
}
});
});
});
告诉我codez – roryf 2010-08-16 13:42:01
粘贴一些代码,请。 – Chris 2010-08-16 13:42:06