简体   繁体   中英

Change attribute depending on Select Option

I want to make the disappear when Other Countries is selected.

This is my HTML

<select class="country" name="country">
     <option value="Portugal">Portugal</option>
     <option value="OtherCountries">Other Countries</option>
</select>

<tr id="this">
     <td valign="middle"><label class="registration">VAT (23%)</label></td>
     <td valign="middle"></td>
     <td valign="middle"><input class="input_form_reg" name="vat_price" type="text"></td>
</tr>

And this is my script (which I found here on stackoverflow)

<script>
    $("select").on('change', function() {
        var sel = $("select").val();
        if (sel=='Portugal') {
            $("#this").css("display", "inline");
        }else if (sel=='OtherCountries') {
            $("#this").css("display", "none");
        }
    });
</script>

And I don't know why but it isn't working, it just stays there.

try

your html is invalid, wrap the tr inside table

<table>
          <tr id="this">
            <td valign="middle"><label class="registration">VAT (23%)</label></td>
            <td valign="middle"></td>
            <td valign="middle"><input class="input_form_reg" name="vat_price" type="text"></td>
          </tr>
    </table>

Working demo

JS

$("select").on('change', function () {
     if (this.value == 'Portugal') {
         $("#this").show();
     } else {
         $("#this").hide();
     }

 });

 $("select").on('change', function () {
     $("#this").toggle(this.value != "OtherCountries");
 });

DEMO

Use this code

<select class="country" name="country">
                <option value="Portugal">Portugal</option>
                <option value="OtherCountries">Other Countries</option>
</select>
<table>
          <tr id="this">
            <td valign="middle"><label class="registration">VAT (23%)</label></td>
            <td valign="middle"></td>
            <td valign="middle"><input class="input_form_reg" name="vat_price" type="text"></td>
          </tr>
          </table>
<script>
    $(".country").change(function() {
        var sel = $(this).val();
        if (sel=='Portugal') {
        $("#this").show();
        }
        if (sel=='OtherCountries') {
        $("#this").hide();
        }
    });
</script>

First change the ID to another one more explicit like "vat" or something, and try this:

$('.country').change(function(){

  if($(this).val() == "OtherCountries"){
    $("#vat").hide();
  }else{
    $("#vat").show();
  }

 });

You can use show/hide instead of using css() method.

Try this:

 $(".country").on('change', function() { if($(this).val()=="OtherCountries"){ $("#this").hide(); }else{ $("#this").show(); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <select class="country" name="country"> <option value="Portugal">Portugal</option> <option value="OtherCountries">Other Countries</option> </select> <table id="this"> <tr> <td valign="middle"><label class="registration">VAT (23%)</label></td> <td valign="middle"></td> <td valign="middle"><input class="input_form_reg" name="vat_price" type="text"></td> </tr> </table> 

Table tag was missing in your HTML due to which it was not identifying table row with ID. I have corrected the code. Please take a look at the code given below.

<select class="country" name="country">
     <option value="Portugal">Portugal</option>
     <option value="OtherCountries">Other Countries</option>
</select>
<table>
<tr id="this">
     <td valign="middle"><label class="registration">VAT (23%)</label></td>
     <td valign="middle"></td>
     <td valign="middle"><input class="input_form_reg" name="vat_price" type="text"></td>
</tr>
</table>
<script>
    $("select").on('change', function() {
        var sel = $("select").val();
        if (sel=='Portugal') {
            $("#this").css("display", "block");
        }else if (sel=='OtherCountries') {
            $("#this").css("display", "none");
        }
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM