如何解决提交时不以隐藏div标签的形式传递值
| 我有控制某些div标签的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\",Value = \"6\"},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\');
您可以编写一个插件来执行此操作,以避免再次重复执行代码,因此您所需要做的就是:
$(\'#divMultipleChoiceQuestion\').toggleVisibility();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。