如果ID丢失,请使用jquery填充另一个隐藏字段中的行
if ID is missing fill the rows in another hiddenfield with jquery
我有一个名为CustomPickedTable
的Table
,这个Table
有具有属性(如<td Data-question-id="5">Example</td>
)的行,有些行根本没有任何属性。仅<td>example</td>.
我想能够将它们分类到不同的隐藏字段,这些是我的隐藏字段:
@Html.HiddenFor(model => model.SelectedCustomQuestions, new { @id = "SelectedQuestionsWithAttr" })
@Html.HiddenFor(model => model.SelectedQuestions, new { @id = "SelectedQuestionsWithNoAttr" })
我现在的代码是,所有具有属性"data-question-id"
的行都被填充到SelectedQuestionsWithAttr
,这是我对具有属性的行的隐藏字段。
但我希望我的Jquery代码也填充那些没有属性的行,并填充到我的SelectedQuestiosnWithNoAttr
隐藏字段中。
这是使用隐藏字段填写所选问题的代码:
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>
标签上添加东西才能识别它们:
<td id="noAttr@(Model.SelectedQuestions.IndexOf(variable))">
那么jQuery将是:
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();
相关文章:
- JavaScript 检查隐藏字段中的值并将其设置为隐藏字段值
- 带有验证和隐藏字段值的提交按钮
- 主干窗体隐藏字段未呈现
- 清除以前的$_GET值或不获取仅隐藏字段的值
- 添加和删除隐藏字段数组中的值,而不提交表单
- 在控制器上使用“$watch”时,为什么不更新此隐藏字段
- 将选项值附加到隐藏字段
- JQuery获取隐藏字段的值
- HTML提交表单,同时包含空字段检查和按钮隐藏
- MVC+访问控制器中的隐藏字段
- 如何隐藏&使用jQuery禁用依赖于另一个字段值的字段
- ASP.net Javascript函数中的隐藏字段为null
- 在第一页加载时隐藏字段,而不是在php发布之后
- 使用JavaScript获取隐藏的Django字段
- 提交表单时,显示或识别隐藏的必填字段并将其集中
- 表单使用js、html,而不是隐藏字段
- HTML5要求隐藏文件字段-反馈位置
- 当javascript中的强制字段隐藏在条件中时,如何限制它们的验证
- 为隐藏字段隐藏jquery验证器消息
- 如何使一些字段隐藏取决于其他字段的值AngularJS, Razor, c#