Javascript Jquery的数组问题
我有一个Jquery代码,如下所示:Javascript Jquery的数组问题,javascript,jquery,asp.net-mvc,Javascript,Jquery,Asp.net Mvc,我有一个Jquery代码,如下所示: var selectedQuestions = $("#SelectedQuestions"); var selectedCustomQuestions = $("#SelectedCustomQuestions"); var currentIds = new Array(); var currentText = new Array(); $("#CustomPickedTable td[data-question-id]").each(function
var selectedQuestions = $("#SelectedQuestions");
var selectedCustomQuestions = $("#SelectedCustomQuestions");
var currentIds = new Array();
var currentText = new Array();
$("#CustomPickedTable td[data-question-id]").each(function () {
var clickedId = $(this).attr("data-question-id");
currentIds.push(clickedId);
$('#CustomPickedTable tr').each(function () {
var ClickedText= $(this).find("td[data-attr-id]:first").html();
currentText.push(ClickedText);
});
});
selectedCustomQuestions.val(currentText.join("|"));
selectedQuestions.val(currentIds.join(","));
$("form").submit();
}
<td data-question-id="7">test</td>
我的表格中有两种类型的TD,如下所示:
var selectedQuestions = $("#SelectedQuestions");
var selectedCustomQuestions = $("#SelectedCustomQuestions");
var currentIds = new Array();
var currentText = new Array();
$("#CustomPickedTable td[data-question-id]").each(function () {
var clickedId = $(this).attr("data-question-id");
currentIds.push(clickedId);
$('#CustomPickedTable tr').each(function () {
var ClickedText= $(this).find("td[data-attr-id]:first").html();
currentText.push(ClickedText);
});
});
selectedCustomQuestions.val(currentText.join("|"));
selectedQuestions.val(currentIds.join(","));
$("form").submit();
}
<td data-question-id="7">test</td>
我的Jquery代码在用currentID填充数组时起作用,但用currentText我会遇到问题,如果我的表中有两个test
,它们会在我的数组列表中重复,我的数组长度是7-10,这很奇怪。CurrentText的长度应仅为2,而不是1。我怎样才能解决这个问题
关于这个问题的例子
我的桌子里有以下内容:
<td data-attr-id="5">aaaa</td>
<td data-attr-id="5">ddd</td>
<td data-question-id="5">test</td>
<td data-question-id="15">test</td>
aaaa
ddd
测试
测试
这就是调试jquery代码时发生的情况
提前谢谢 您似乎在查找选择器中使用“td[data attr id]:first”,但在html中使用的是“data attri id”,请注意添加的i
编辑:
你也可以使用
var currentIds = [];
var currentText = [];
而不是:
var currentIds = new Array();
var currentText = new Array();
见:
只要稍微修改一下就可以试试这个
var selectedQuestions = $("#SelectedQuestions");
var selectedCustomQuestions = $("#SelectedCustomQuestions");
var currentIds = new Array();
var currentText = new Array();
$("#CustomPickedTable td[data-question-id]").each(function () {
var clickedId = $(this).attr("data-question-id");
currentIds.push(clickedId);
});
$('#CustomPickedTable td[data-attr-id]').each(function () {
var ClickedText = $(this).html();
currentText.push(ClickedText);
});
selectedCustomQuestions.val(currentText.join("|"));
selectedQuestions.val(currentIds.join(","));