最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jQuery showhide text field based on a dropdown selection value and a radiobutton - Stack Overflow

matteradmin6PV0评论

I'm trying to show/hide a hidden field based on a dropdown selection value and a radiobutton value.

Here's part of my code:

State of Issue: 
<select size="1" name="State_of_Issue" class="required" id="state">
<option>MA</option> 
<option>NH</option>
<option>NY</option>
</select>

Partnership:
<input type="radio" name="Partnership" value="Yes" class="required" />Yes
<input type="radio" name="Partnership" value="No" />No

<select name="Asset_Number" id="asset" style="display:none;">
                <option>Total Asset 100%</option>
                <option>Total Asset 50%</option>
                <option>Dollar for Dollar 100%</option>
                <option>Dollar for Dollar 50%</option>
            </select>

Script:
$('#state').change(function () {
   if ($(this).val() == "NY") {
     if ($('#partnership').val() == "Yes") {
        $('#asset').show();                        }
       }
       else {
           $('#asset').hide();    
          }
       });

I want to be show the hidden field "asset" when state = NY and when partnership = Yes.

I'm trying to show/hide a hidden field based on a dropdown selection value and a radiobutton value.

Here's part of my code:

State of Issue: 
<select size="1" name="State_of_Issue" class="required" id="state">
<option>MA</option> 
<option>NH</option>
<option>NY</option>
</select>

Partnership:
<input type="radio" name="Partnership" value="Yes" class="required" />Yes
<input type="radio" name="Partnership" value="No" />No

<select name="Asset_Number" id="asset" style="display:none;">
                <option>Total Asset 100%</option>
                <option>Total Asset 50%</option>
                <option>Dollar for Dollar 100%</option>
                <option>Dollar for Dollar 50%</option>
            </select>

Script:
$('#state').change(function () {
   if ($(this).val() == "NY") {
     if ($('#partnership').val() == "Yes") {
        $('#asset').show();                        }
       }
       else {
           $('#asset').hide();    
          }
       });

I want to be show the hidden field "asset" when state = NY and when partnership = Yes.

Share Improve this question edited Apr 18, 2013 at 21:29 user2296886 asked Apr 18, 2013 at 21:16 user2296886user2296886 31 silver badge3 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 2

DEMO

$('#state, input[type="radio"]').change(checkState);

function checkState() {
    var state = $('select').val();
    var partnership = $('input[type="radio"]:checked').val();

    if ((state == "NY") && (partnership == "Yes")) {
        $('#asset').show();
    } else {
        $('#asset').hide();
    }
}

Try this script. UPDATED

$('#state, input[type="radio"]').change(function () {
    if ($('#state').val() == "NY") {
        if ($('#partnership').is(':checked')) {
            $('#asset').show();
        } else {
            $('#asset').hide();
        }
    } else {
        $('#asset').hide();
    }
});

And a fiddle > http://jsfiddle/Spokey/Tu7Ja/5/

Post a comment

comment list (0)

  1. No comments so far