开发者

Not pass values from hidden div tag in a form when submitted

开发者 https://www.devze.com 2023-03-09 05:14 出处:网络
I have javascript that controls certain div tags that hide or show upon checked state. I then pass these values when i submit form. Thing is, values from fields in the div tag that were hidden get pas

I have javascript that controls certain div tags that hide or show upon checked state. I then pass these values when i submit form. Thing is, values from fields in the div tag that were hidden get passed as well. How do i not pass these values?

My HTML / Javascript is as follows :

<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("Questio开发者_JAVA技巧nType", new List<SelectListItem>
                     {
                        new SelectListItem{Text="Numeric", Value = "6"}, 
                        new SelectListItem{Text="Alphanumeric", Value = "7"}                                                                                  
                     }) %>
</div>

And my Javascript is something like:

$("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();
    }            
});

So how would one go about not passing empty or any elements in a hidden div tag on a form submit?


In addition to hiding the div you could disable the input elements inside it and they will not be sent to the server:

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

Don't forget to re-enable them when you show the div:

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

You could write a plugin that will do this to avoid repeating the code all over again, so that all you need to do is:

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

精彩评论

暂无评论...
验证码 换一张
取 消