简体   繁体   English

使用select选项时如何使用jQuery隐藏和显示效果

[英]How to use jQuery hide and show effect when using select option

i got one more doubt im close to the answer but not getting it to worked, Actually i have the a default input text & default drop-down(drop-down which consist of west Bengal & others). 我还有一个疑问,我很接近答案,但没有解决,实际上我有默认的输入文本和默认的下拉菜单(下拉菜单包括西孟加拉邦和其他)。 NOW if some one click's on the west Bengal state under drop-down then the default input should get hide and the west Bengal drop-down should get displayed.Below is the code what i have tried.can any one please guideme a bit new to jQuery. 现在,如果在下拉菜单中单击某一个东西,那么默认输入应该隐藏起来,并且应该显示该下拉菜单。下面是我尝试过的代码。 jQuery的。

thanks!! 谢谢!!

                                            <div class="col-sm-4">
                                                <div class="form-group">
                                                    <select id="state" name="state" class="form-control" required="true" autocomplete="false" style="margin-bottom:10px">
                                                        <option value="" disabled="" selected="">Select State</option>
                                                        <option value="WestBengal">West Bengal</option>
                                                        <option value="Others">Others</option>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="col-sm-4">
                                                <div class="form-group otherdistricts">
                                                    <input class="form-control form-control-lg" type="text" name="other_district" id="other_district" placeholder="Enter Your District"  autocomplete="false">
                                                </div>

                                                <div class="westbengaldistrict"  style="display:none"> 
                                                    <select class="form-control" name="district" id="district" autocomplete="false">
                                                        <option value="" selected disabled>Select Your District</option>
                                                        <option value="Alipurduar">Alipurduar</option>
                                                        <option value="Bankura">Bankura</option>
                                                        <option value="PaschimBardhaman">Paschim Bardhaman</option>
                                                        <option value="PurbaBardhaman">Purba Bardhaman</option>
                                                        <option value="Birbhum">Birbhum</option>
                                                        <option value="CoochBehar">Cooch Behar</option>
                                                        <option value="Darjeeling">Darjeeling</option>
                                                        <option value="UttarDinajpur">Uttar Dinajpur</option>
                                                        <option value="DakshinDinajpur">Dakshin Dinajpur</option>
                                                        <option value="Hooghly">Hooghly</option>
                                                        <option value="Howrah">Howrah</option>
                                                        <option value="Jalpaiguri">Jalpaiguri</option>
                                                        <option value="Jhargram">Jhargram</option>
                                                        <option value="UttarDinajpur">Uttar Dinajpur</option>
                                                        <option value="Kalimpong">Kalimpong</option>
                                                        <option value="Malda">Malda</option>
                                                        <option value="PaschimMedinipur">Paschim Medinipur</option>
                                                        <option value="PurbaMedinipur">Purba Medinipur</option>
                                                        <option value="Murshidabad">Murshidabad</option>
                                                        <option value="Nadia">Nadia</option>
                                                        <option value="North24Parganas">North 24 Parganas</option>
                                                        <option value="South24Parganas">South 24 Parganas</option>
                                                        <option value="Purulia">Purulia</option>
                                                    </select>
                                                </div>
                                            </div>

                                            <script>
                                                 $(document).ready(function(){
                                                     $("#state").on("select",function() {
                                                         if ($(this).val() === "WestBengal") {
                                                            $(".otherdistricts").hide();
                                                            $(".westbengaldistrict").show();
                                                        }
                                                     });
                                                 });
                                            </script>

You have errors in the jquery code. jQuery代码中有错误。 Use the below code, it is working fine and tested. 使用以下代码,它可以正常工作并经过测试。

<script>

           $(document).ready(function(){
              $("select").change(function(){
                    $( "select option:selected").each(function(){
                        //enter bengal districts
                        if($(this).attr("value")=="WestBengal"){
                            $(".enterotherstates").hide();
                            $(".enterbengaldistricts").show();
                        }
                        //enter other states
                        if($(this).attr("value")=="Others"){
                            $(".enterbengaldistricts").hide();
                            $(".enterotherstates").show();
                        }
                    });
                });  
            }); 

        </script>

answer to your updated question 回答您更新的问题

$(document).ready(function(){
$(".westbengaldistrict").hide(); // first hide the select
$("#state").on("change",function() {
       if ($(this).val() === "WestBengal") {
          $(".otherdistricts").hide();
          $(".westbengaldistrict").show(); // show on change
      }
   });
});

Your closing parentheses and brackets are the other way around in your javaScript code. 在您的javaScript代码中,右括号和方括号是相反的。

Should be like this 应该是这样的

$(document).ready(function () { code here });

But you have 但是你有

$(document).ready(function () { code here )};

Try this 尝试这个

 <script>
      $(document).ready(function () {
        $("select").change(function () {
          $("select option:selected").each(function () {
            //enter bengal districts
            if ($(this).attr("value") == "WestBengal") {
              $(".enterotherstates").hide();
              $(".enterbengaldistricts").show();
            }
            //enter other states
            if ($(this).attr("value") == "Others") {
              $(".enterbengaldistricts").hide();
              $(".enterotherstates").show();
            }
          });
      });
    });
</script>
$(document).ready(function () {
  $("select").change(function () {
    if ($(this).val() === "WestBengal") {
      $(".enterotherstates").hide();
      $(".enterbengaldistricts").show();
    }
    else {
      $(".enterbengaldistricts").hide();
      $(".enterotherstates").show();
    }
  });
});

Your code has error. 您的代码有错误。 Your code is: 您的代码是:

$(document).ready(function())};

Change to: 改成:

$(document).ready(function()});

Since select is a form element you can right use val() method instead of $(this).attr("value") to get value. 由于select是一个表单元素,因此可以使用val()方法代替$(this).attr("value")来获取值。

You have several issues in your code. 您的代码中有几个问题。 First of all, your closing brackets are wrong. 首先,您的右括号是错误的。 You should have )}; 你应该有 )}; instead of }); 代替 });

Your inputfield has display: none, you should remove that, because $(".enterotherstates").show(); 您的输入字段显示:没有,您应该删除它,因为$(“。enterotherstates”)。show(); won't let it appear, only the parent element. 不会让它出现,只有父元素。

Next, you don't need to iterate with each. 接下来,您不需要迭代每个对象。 It's easier and faster to use $(this). 使用$(this)更容易,更快。

$(document).ready(function() {
    $("select").change(function() {
        if ($(this).val() === "WestBengal") {
            $(".enterotherstates").hide();
            $(".enterbengaldistricts").show();
        } else {
            $(".enterbengaldistricts").hide();
            $(".enterotherstates").show();
        }

    });
});

Optional performance advice 可选的性能建议

you could make your code faster by caching your jQuery objects and reuse them. 您可以通过缓存jQuery对象并重用它们来提高代码速度。 So the browser doesn't have to search the complete HTML again and again: 因此,浏览器不必一次又一次地搜索完整的HTML:

$(document).ready(function() {

    var otherStates = $(".enterotherstates");
    var bengal = $(".enterbengaldistricts");

    $("select").change(function() {
        if ($(this).val() === "WestBengal") {
            otherStates.hide();
            bengal.show();
        } else {
            bengal.hide();
            otherStates.show();
        }

    });
});

Below is the updated jquery code as per the updated question. 以下是根据更新的问题更新的jquery代码。 It is working fine and tested. 它工作正常并经过测试。

 $(document).ready(function(){
 $("#state").change(function() {
         if ($(this).val() === "WestBengal") {
            $(".otherdistricts").hide();
            $(".westbengaldistrict").show();
        }else{
           $(".otherdistricts").show();
            $(".westbengaldistrict").hide();
        }
     });
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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