问题描述
|
我有控制某些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();