简体   繁体   English

.toggle功能无法正常工作

[英].toggle function not working correctly

I use the script below to toggle the County menu, I am trying to get it to hide the county menu if on page load any country other the "United Kingdom" is selected and if the user selects another country once the page has loaded, problem is if "United Kingdom" is selected when the page loads the "county" menu is still hidden and you have to select another country first then select back to "United Kingdom to get the County list to show. 我使用下面的脚本来切换县菜单,我试图让它隐藏县菜单,如果在页面上加载任何国家,其他“英国”被选中,如果用户选择另一个国家,一旦页面加载,问题如果在页面加载时选择“英国”,则“县”菜单仍然隐藏,您必须先选择另一个国家,然后选择返回“英国才能显示县名单”。

<script type="text/javascript">
    $(document).ready(function(){
       $(".selectWrapper").on("change", "select#country", function(){
          $("select.county").toggle($(this).val() == "United Kingdom");
       });
    });

    </script>



    <script type="text/javascript">
    $(document).ready(function() {
           if($("select.country") != "United Kingdom") {
          $("select.county").toggle();
          }
    });
    </script>

You are missing the call of your .val() in the second if statement ! 您在第二个if语句中缺少.val()的调用! You are currently comparing an jQuery Object with a String. 您当前正在将jQuery对象与String进行比较。

<script type="text/javascript">
    $(document).ready(function(){
       $(".selectWrapper").on("change", "select#country", function(){
          $("select.county").toggle($(this).val() == "United Kingdom");
       });
    });

    </script>



    <script type="text/javascript">
    $(document).ready(function() {
           if($("select.country").val() != "United Kingdom") {
          $("select.county").toggle();
          }
    });
    </script>

You need to get the selected value of the select, it is done with the val() function: 您需要获取select的选定 ,它使用val()函数完成:

$(document).ready(function() {
    if ($("select.country").val() != "United Kingdom") {
        $("select.county").toggle();
    }
});​

By the way, you're creating a delegate event of change with the on function: 顺便说一句,您正在使用on函数创建一个委托change事件:

$(".selectWrapper").on("change", "select#country",

Pretty sure that you don't have to... 很确定你不必......

Why not: 为什么不:

$("select#country").change(function(){
      $("select.county").toggle($(this).val() == "United Kingdom");
   });

Use this to get the initial state correct and respond to changes: 使用此选项可以使初始状态正确并响应更改:

$(document).ready(function(){
   $(".selectWrapper").on("change", "#country", function(){
      $("select.county").toggle($(this).val() == "United Kingdom");
   });
   // set the initial state
   $("select.county").toggle($("#country").val() == "United Kingdom");
});

This assumes that the #country object exists at page load time. 这假设#country对象在页面加载时存在。 If it doesn't exist yet, then you will have to trigger the initialization code at some point after it is loaded and does exist. 如果它还不存在,那么你必须在加载并存在之后的某个时刻触发初始化代码。

Note, there is no reason in this context to use "select#country" as the selector when "#country" will work just as well yet be faster for the selector engine to evaluate. 注意,在这种情况下,没有理由使用"select#country"作为选择器,当"#country"同样适用时,选择器引擎的评估速度会更快。

You want to toggle first when the DOM is ready, to set the proper initial state. 您希望在DOM准备就绪时首先切换,以设置正确的初始状态。 Then you want to toggle every time the select changes. 然后,您希望每次选择更改时切换。 It would be something like: 它会是这样的:

$(document).ready(function(){
   // Toggle on DOM-ready
   $("select.county").toggle($("#country").val() == "United Kingdom");

   // Then again on change
   $(".selectWrapper").on("change", "select#country", function(){
      $("select.county").toggle($("#country").val() == "United Kingdom");
   });
});

if there is two drop downs (one with an ID and one with a class name) then you could do: 如果有两个下拉(一个带有ID,一个带有类名),那么你可以这样做:

$(document).ready(function(){
    $(".selectWrapper").on("change", "select#country", function(){
        $("select.county").toggle($(this).val() == "United Kingdom");
    });
    $("select#country").trigger("change");
});

using .trigger to do everything the select#country will run (if you are doing more then just toggle visibility on select.country sample: http://jsfiddle.net/x23nT/ 使用.trigger执行select#country将运行的所有内容(如果您正在执行更多操作,那么只需在select.country示例上切换可见性: http//jsfiddle.net/x23nT/

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

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