简体   繁体   中英

show field only if 2 conditions are true?

This is a simple question but could someone help me with javascript which will show a div based on two dropdown selections being true?

This is the code I have but I just want to know if this is correct?

EDIT SOLVED with both answers below. Thanks a lot guys!

 <script> function myFunction(){ $('#LoadingPlace,#DeliveryPlaces').change(function () { if ($('#DeliveryPlaces').val() == '1' || ["Seaport 1", "Seaport 2", "Seaport 3"].indexOf($('#LoadingPlace').val()) > -1) { $("{#ContainerSize1").show(); $("#ContainerFeature1").show(); $("#Genset1").show(); } else { $("{#ContainerSize1").hide(); $("#ContainerFeature1").hide(); $("#Genset1").hide(); } })}; </script> 

use && (its allow only both are true) instead of || (its allow any one true)

<script>
function myFunction(){
$('#LoadingPlace,#DeliveryPlaces').change(function () {
    if (($('#DeliveryPlaces').val() == '1') && (["Seaport 1", "Seaport 2", "Seaport 3"].indexOf($('#LoadingPlace').val()) > -1)) {
        $("{#ContainerSize1").show();
        $("#ContainerFeature1").show();
        $("#Genset1").show();            
        } 

    else {
        $("{#ContainerSize1").hide();
        $("#ContainerFeature1").hide();
        $("#Genset1").hide();  
    }    

    })};
</script>

You can try this : jsfiddle.net/bharatsing/3y8n9msc/2/

Also in you code I have found that

$("{#ContainerSize1").show();

This should be

$("#ContainerSize1").show();

$(document).ready(function(){
  $('#LoadingPlace,#DeliveryPlaces').change(function () { 
      if ($('#DeliveryPlaces').val() == '1' && 
              ["Seaport 1", "Seaport 2", "Seaport 3"].indexOf($('#LoadingPlace').val()) > -1) {
          $("#ContainerSize1").show();
          $("#ContainerFeature1").show();
      $("#Genset1").show();            
      } 

    else {
          $("#ContainerSize1").hide();
          $("#ContainerFeature1").hide();
      $("#Genset1").hide();  
      }
    });
});

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