Javascript 如果缺少ID,则使用jquery填充另一个hiddenfield中的行
我有一个名为Javascript 如果缺少ID,则使用jquery填充另一个hiddenfield中的行,javascript,jquery,asp.net-mvc,Javascript,Jquery,Asp.net Mvc,我有一个名为CustomPickedTable的表,该表具有属性的行,例如示例,其中一些行根本没有任何属性。只需示例。 我希望能够将它们分类为不同的隐藏字段,这些是我的隐藏字段: @Html.HiddenFor(model => model.SelectedCustomQuestions, new { @id = "SelectedQuestionsWithAttr" }) @Html.HiddenFor(model => model.SelectedQuestions, new
CustomPickedTable
的表
,该表
具有属性的行,例如示例
,其中一些行根本没有任何属性。只需示例。
我希望能够将它们分类为不同的隐藏字段,这些是我的隐藏字段:
@Html.HiddenFor(model => model.SelectedCustomQuestions, new { @id = "SelectedQuestionsWithAttr" })
@Html.HiddenFor(model => model.SelectedQuestions, new { @id = "SelectedQuestionsWithNoAttr" })
我现在的代码是,所有具有属性的行“数据问题id”
都被填充到SelectedQuestionsWithAttr
,这是具有属性的行的隐藏字段
但是我希望我的Jquery代码也会填充那些没有属性的行,并填充到我的selectedquestiosnwhithnoattr
hiddenfield
这是仅填写选定问题的代码,该问题包含TR HIDDEN字段:
var selectedQuestionsWithAttr = $("#SelectedQuestionsWithAttr");
var currentIds = new Array();
$("#CustomPickedTable").find("td").each(function () {
var clickedId = $(this).attr("data-question-id");
currentIds.push(clickedId);
});
selectedQuestionsWithAttr.val(currentIds.join(","));
$("form").submit();
}
有什么解决方案可以添加到jquery代码中吗
提前感谢您需要在
标签上添加一些内容,以便能够识别它们:
<td id="noAttr@(Model.SelectedQuestions.IndexOf(variable))">
应该可以只使用“if tables td没有任何属性do this”,这就是td:not([data question id])选择器的作用。您在该函数中的操作由您决定,但您确实要求将这些行添加到您选择的问题中,而不使用TTR变量,因此,需要以某种方式标识这些行。
var $qWithAttr = $("#SelectedQuestionsWithAttr");
var $qWithoutAttr = $("#SelectedQuestionsWithNoAttr");
var currentIds = new Array();
var missingIds = new Array();
$("#CustomPickedTable td[data-question-id]").each(function () {
currentIds.push($(this).attr("data-question-id"));
});
$("#CustomPickedTable td:not([data-question-id])").each(function () {
missingIds.push($(this).attr("id"));
});
$qWithAttr.val(currentIds.join(","));
$qWithoutAttr.val(missingIds.join(","));
$("form").submit();