提交时,不传递表单中隐藏div标签的值

Hri*_*kan 3 html javascript asp.net-mvc jquery

我有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>
Run Code Online (Sandbox Code Playgroud)

而我的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();
    }            
});
Run Code Online (Sandbox Code Playgroud)

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

Dar*_*rov 6

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

$('#divMultipleChoiceQuestion').hide();
$('#divMultipleChoiceQuestion :input').attr('disabled', 'disabled');
Run Code Online (Sandbox Code Playgroud)

当您显示div时,不要忘记重新启用它们:

$('#divMultipleChoiceQuestion').show();
$('#divMultipleChoiceQuestion :input').removeAttr('disabled');
Run Code Online (Sandbox Code Playgroud)

您可以编写一个插件来执行此操作以避免重复重复代码,因此您需要做的就是:

$('#divMultipleChoiceQuestion').toggleVisibility();
Run Code Online (Sandbox Code Playgroud)

  • @Patricia,[:input](http://api.jquery.com/input-selector/)选择器包含`<select>`元素,所以没问题. (2认同)