Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
1.18/5 (3 votes)
See more:
I am fetching value in dropdownlist like a,b etc but there are 5 textboxes. i did code for displaying 3 textboxes on 'a selection and 2 on b selection but bydefault 'a' coming in dropdownlist list i want to show 3 textboxes on 'a' seleted in dropdownlist. plz help me. i am using mvc with razor
Posted
Updated 16-Jun-14 20:15pm
v2
Comments
OriginalGriff 17-Jun-14 1:50am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
SRS(The Coder) 17-Jun-14 3:16am    
You marked my answer downvoted, can you please tell me if i can help you in any regards to improve the solution ?

1 solution

As per the scenario described by you, i have prepared a small test implementation as below :-

I have one dropdown and 5 text boxes in my page as below :-
Ex :
HTML
<select id="dpdTest" onchange="DisplayTextBoxes()">
            <option value="1">a</option>
            <option value="2">b</option>
        </select>
        <input type="text" name="txtTest1" id="txtTest1" style="display:none;" />
        <input type="text" name="txtTest1" id="txtTest2" style="display:none;" />
        <input type="text" name="txtTest1" id="txtTest3" style="display:none;" />
        <input type="text" name="txtTest2" id="txtTest4" style="display:none;" />
        <input type="text" name="txtTest2" id="txtTest5" style="display:none;" />


All textboxes a re made invisible by default.

Now here is the jQuery code i have to conditionally display the textboxes :-
Ex :
JavaScript
$(function () {
            DisplayTextBoxes();
        });
        function DisplayTextBoxes() {
            //Use $("#dpdTest").val() to check with the values i.e. 1, 2 etc.
            if ($("#dpdTest option:selected").text() == 'a') {
                $('input[name=txtTest1]').css("display", "block");
                $('input[name=txtTest2]').css("display", "none");
            }
            else {
                $('input[name=txtTest1]').css("display", "none");
                $('input[name=txtTest2]').css("display", "block");
            }
        }


Here i have grouped the textboxes by giving same name attribute value.
We may use the textbox IDs to perform the display separately for each textboxes.


Hope this will definitely of use to you.
 
Share this answer
 
Comments
Member 9027346 17-Jun-14 4:09am    
i did same code but suppose if a is showing in dropdownlist on page load then it should work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900