gpt4 book ai didi

javascript - 获取循环中变量的值

转载 作者:行者123 更新时间:2023-12-02 20:34:46 26 4
gpt4 key购买 nike

我有一个循环,当它被调用时,会创建一个包含一些表单元素的 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 &amp; 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">&nbsp;</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">&nbsp;</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>');
}
}
});
});
});

最佳答案

您可以将需要引用正确版本的 i 的代码放置在如下闭包中:

var i = 0;

$('a#add-product').click(function(event){
i++;

// Begin closure. When called (at the end of the closure) it receives
// the current value of "i". This value of "i" will be referenced
// throughout the closure as a local variable containing the value
// you expect, instead of the "shared" "i" variable outside the
// closure.
(function( i ) {

// So basically we've created a new "scope" inside here. Now "i"
// is a separate local variable than the "i" variable ouside
// the closure. You could change the variable name by changing
// the parameter above. Like (function( my_i ) {...
// If you did that, you would need to change the "i" in your .change()
// handlers to "my_i". The rest of them could stay the same, or you
// could change them. Either way would work.
// This is because the .change() handlers are executed at a later time
// (and so are the AJAX callbacks) so they need to use the variable
// that is local to this closure.
// The rest of the code, like $("#selectionresult-" + i) is executing
// immediately, so it could reference the "i" variable that is
// outside the closure, and still work properly.

$('<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 &amp; 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">&nbsp;</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">&nbsp;</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>');
}
}
});
});

// End closure. Executes the closure function, passing in the
// current value of "i"
})( i );
});
<小时/>

编辑:

为了解释正在发生的事情,在 javascript 中,传递到(或在函数体中创建)的变量是该函数的本地变量,并且它们会持续存在。

我上面所做的只是创建一个接受一个参数的函数。在这里,我将更改参数的名称,以便使其更清晰:

function( inner_i ) {
// create your element with the new local variable "inner_i"
}

...但我也会在创建该函数后立即调用该函数:

(function( inner_i ) {
// create your element with the new local variable "inner_i"
})( i )
// ^------- call the function, passing in the "i" from your loop.

语法看起来有点奇怪,但这只是调用您刚刚创建的函数的一种方法。

这与执行以下操作相同:

function myNewFunction( inner_i ) {
// creates your element with the new local variable "inner_i"
}

myNewFunction( i ); // Call the function we just created above,
// and pass the "i" from the loop into it

关于javascript - 获取循环中变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3493668/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com