我有 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/

10-13 01:38
查看更多