gpt4 book ai didi

javascript - jQuery 每个循环只给出第一个结果

转载 作者:行者123 更新时间:2023-11-28 17:01:35 24 4
gpt4 key购买 nike

我正在尝试生成一个数组,通过 jQuery 提交给 ajax 调用。这是我的 HTML 和 JavaScript:

//Following is the JavaScript I use to generate the variables from checked checkboxes

jQuery(document).on("click", ".refno", function() {
var ref = jQuery(this).data("ref");
var refindex = jQuery(this).data("refindex");

var i = 0;
var tutelists = [];
var tutearray = {};
jQuery('.datarefindex_' + refindex).each(function() {
var subjid = jQuery(this).data("subjid");
i++;

tutearray["id"] = subjid;
tutearray["issuedtutes"] = jQuery("input[name=tuteset_" + refindex + "_" + subjid + "]:checked").map(function() {
return this.value;
}).get();

});
tutelists.push(tutearray);
var tutelists = JSON.stringify(tutelists);

jQuery("#responsecontainer").html(tutelists);

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<a href="javascript:void(0)" class="refno" data-refindex="1" data-ref="1105898">1105898</a>
<ul>
<li>
<div>Title 1</div>
<ul class="datarefindex_1" data-subjid="opt471821">
<li>
<label class="tutebtn" for="1_opt471821_1">
<input name="tuteset_1_opt471821" data-mainrefindex="1" type="checkbox" id="1_opt471821_1" data-subj="opt471821" value="1"> 1</label>
</li>
<li>
<label class="tutebtn" for="1_opt471821_2">
<input name="tuteset_1_opt471821" data-mainrefindex="1" type="checkbox" id="1_opt471821_2" data-subj="opt471821" value="2"> 2</label>
</li>
</ul>
</li>
<li>
<div>Title 2</div>
<ul class="datarefindex_1" data-subjid="opt1215754">
<li>
<label class="tutebtn" for="1_opt1215754_1">
<input name="tuteset_1_opt1215754" data-mainrefindex="1" type="checkbox" id="1_opt1215754_1" data-subj="opt1215754" value="1"> 1</label>
</li>
<li>
<label class="tutebtn" for="1_opt1215754_2">
<input name="tuteset_1_opt1215754" data-mainrefindex="1" type="checkbox" id="1_opt1215754_2" data-subj="opt1215754" value="2"> 2</label>
</li>
</ul>
</li>
<li>
<div>Title 3</div>
<ul class="datarefindex_1" data-subjid="opt3062581">
<li>
<label class="tutebtn" for="1_opt3062581_1">
<input name="tuteset_1_opt3062581" data-mainrefindex="1" type="checkbox" id="1_opt3062581_1" data-subj="opt3062581" value="1"> 1</label>
</li>
<li>
<label class="tutebtn" for="1_opt3062581_2">
<input name="tuteset_1_opt3062581" data-mainrefindex="1" type="checkbox" id="1_opt3062581_2" data-subj="opt3062581" value="2"> 2</label>
</li>
</ul>
</li>
</ul>
<div id="responsecontainer"></div

>

我收到的结果如下(我已选中每个列表中的前 3 个框);

{"id":"opt3062581","issuedtutes1":["1","2","3"],
"issuedtutes2":["1","2","3"],
"issuedtutes3":["1","2","3"]}

但是期望的结果如下;

{"id":"opt471821","issuedtutes":["1","2","3"]},
{"id":"opt1215754","issuedtutes":["1","2","3"]},
{"id":"opt3062581","issuedtutes":["1","2","3"]}

无法弄清楚我错过了什么。当我从函数中删除 id 部分时,返回 {"id":"opt3062581","issuedtutes":["1","2","3"]} 这是最后一个列表中的数据。请帮忙。

类(class)列表的数量是动态的且不固定。

最佳答案

您的预期输出看起来像一个对象列表,因此 tutelists 应该是一个列表而不是一个对象。

jQuery(document).on("click", ".refno", function() {
var ref = jQuery(this).data("ref");
var refindex = jQuery(this).data("refindex");

var i = 0;
var tutelists = [];
jQuery('.datarefindex_' + refindex).each(function() {
var subjid = jQuery(this).data("subjid");
i++;
var tutearray = {};

tutearray["id"] = subjid;
tutearray["issuedtutes"] = jQuery("input[name=tuteset_" + refindex + "_" + subjid + "]:checked").map(function() {
return this.value;
}).get();
tutelists.push(tutearray);

});
var tutelists = JSON.stringify(tutelists);

jQuery("#responsecontainer").html(tutelists);

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="javascript:void(0)" class="refno" data-refindex="1" data-ref="1105898">1105898</a>
<ul>
<li>
<div>Title 1</div>
<ul class="datarefindex_1" data-subjid="opt471821">
<li>
<label class="tutebtn" for="1_opt471821_1">
<input name="tuteset_1_opt471821" data-mainrefindex="1" type="checkbox" id="1_opt471821_1" data-subj="opt471821" value="1"> 1</label>
</li>
<li>
<label class="tutebtn" for="1_opt471821_2">
<input name="tuteset_1_opt471821" data-mainrefindex="1" type="checkbox" id="1_opt471821_2" data-subj="opt471821" value="2"> 2</label>
</li>
</ul>
</li>
<li>
<div>Title 2</div>
<ul class="datarefindex_1" data-subjid="opt1215754">
<li>
<label class="tutebtn" for="1_opt1215754_1">
<input name="tuteset_1_opt1215754" data-mainrefindex="1" type="checkbox" id="1_opt1215754_1" data-subj="opt1215754" value="1"> 1</label>
</li>
<li>
<label class="tutebtn" for="1_opt1215754_2">
<input name="tuteset_1_opt1215754" data-mainrefindex="1" type="checkbox" id="1_opt1215754_2" data-subj="opt1215754" value="2"> 2</label>
</li>
</ul>
</li>
<li>
<div>Title 3</div>
<ul class="datarefindex_1" data-subjid="opt3062581">
<li>
<label class="tutebtn" for="1_opt3062581_1">
<input name="tuteset_1_opt3062581" data-mainrefindex="1" type="checkbox" id="1_opt3062581_1" data-subj="opt3062581" value="1"> 1</label>
</li>
<li>
<label class="tutebtn" for="1_opt3062581_2">
<input name="tuteset_1_opt3062581" data-mainrefindex="1" type="checkbox" id="1_opt3062581_2" data-subj="opt3062581" value="2"> 2</label>
</li>
</ul>
</li>
</ul>
<div id="responsecontainer"></div

>

关于javascript - jQuery 每个循环只给出第一个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57271299/

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