gpt4 book ai didi

javascript - 提交时不传递表单中隐藏的 div 标记的值

转载 作者:搜寻专家 更新时间:2023-10-31 22:50:40 25 4
gpt4 key购买 nike

我有 javascript 来控制某些 div 标签,这些标签在选中状态时隐藏或显示。然后我在提交表单时传递这些值。事实上,div 标记中隐藏的字段的值也会被传递。我如何不传递这些值?

我的 HTML/Javascript 如下:

<div id="divShortAnswerQuestion">
<h2>Short Answer Question</h2>
<!--Question Order-->
<asp:Label ID="Label2" runat="server" Text="Question Order"></asp:Label>
<%=Html.TextBox("QuestionOrder")%>
<br />
<!--Partial Answers-->
<asp:Label ID="Label5" runat="server" Text="Allow Partial Answers"></asp:Label>
<%=Html.RadioButton("PartialAnswers", "1", true) %>Yes
<%=Html.RadioButton("PartialAnswers", "0", false) %>No
<br />
<!--Max Answers-->
<asp:Label ID="Label6" runat="server" Text="Max Answers (1-10)"></asp:Label>
<%=Html.TextBox("Max") %>
<br />
<!--Data Type-->
<asp:Label ID="Label7" runat="server" Text="Data Type"></asp:Label>
<%=Html.DropDownList("DataType", new List<SelectListItem>
{
new SelectListItem{Text="Numeric", Value = "Numeric"},
new SelectListItem{Text="Alphanumeric", Value = "Alphanumeric"},
new SelectListItem{Text="Date", Value = "Date"}
}) %>
<br />
<!--Question Type-->
<asp:Label ID="Label13" runat="server" Text="Question Type"></asp:Label>
<%=Html.DropDownList("QuestionType", new List<SelectListItem>
{
new SelectListItem{Text="Numeric", Value = "6"},
new SelectListItem{Text="Alphanumeric", Value = "7"}
}) %>
</div>

我的 Javascript 是这样的:

$("input:radio[@name=QuestionGroup]").click(function() {
var checkedvalue = $(this).val();
if(checkedvalue=="1"){
$('#divShortAnswerQuestion').hide();
$('#divMultipleChoiceQuestion').show();
$('#divParticipantListQuestion').hide();
}
if(checkedvalue=="2"){
$('#divMultipleChoiceQuestion').hide();
$('#divShortAnswerQuestion').show();
$('#divParticipantListQuestion').hide();
}
if(checkedvalue=="3"){
$('#divMultipleChoiceQuestion').hide();
$('#divShortAnswerQuestion').hide();
$('#divParticipantListQuestion').show();
}
});

那么,如何才能不在表单提交时传递空元素或隐藏的 div 标签中的任何元素呢?

最佳答案

除了隐藏 div 之外,您还可以禁用其中的输入元素,它们将不会被发送到服务器:

$('#divMultipleChoiceQuestion').hide();
$('#divMultipleChoiceQuestion :input').attr('disabled', 'disabled');

不要忘记在显示 div 时重新启用它们:

$('#divMultipleChoiceQuestion').show();
$('#divMultipleChoiceQuestion :input').removeAttr('disabled');

你可以 write a plugin这样做是为了避免再次重复代码,因此您需要做的就是:

$('#divMultipleChoiceQuestion').toggleVisibility();

关于javascript - 提交时不传递表单中隐藏的 div 标记的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6152353/

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