简体   繁体   English

如何将功能应用于选项选择标签?

[英]How to apply a function to option selected tag?

I have the following dropdown: 我有以下下拉列表:

    <select id="role" name="role" class="form-control">
      <option selected>Choose an option</option>
      {% for index, value in roles_form.fields.roles.choices %}
          <option value="{{ index }}">{{ value }}</option>
      {% endfor %}
    </select>

And I have a function that show and hides another div when the user chose between the options: 当用户在选项之间进行选择时,我有一个显示和隐藏另一个div的函数:

$('select').on('change', function() {
      var developer = {{ developer_id }};
      var value = $(this).val();
      console.log(developer);

      if (value == developer) {
        $('#github_link').show();
      } else {
        $('#github_link').hide();
      }
})

So, when the user choose Developer the div remains shown, and when they choose any other option this div hides. 因此,当用户选择Developer ,div仍会显示,当他们选择任何其他选项时,该div隐藏。 Everything works fine, but there's a thing, when the page loads this tag: <option selected>Choose an option</option> mantain the div shown, and that's not correct, when the page loads the div needs to be hidden too. 一切正常,但有一件事,当页面加载此标签时: <option selected>Choose an option</option>保持显示的div,这是不正确的,当页面加载div时也需要隐藏。

However I'm not pretty sure how to add that to the function, any thoughts? 但是我不太确定如何将其添加到函数中,任何想法?

If your issue is that you want this logic to run on page load, as well as on change, then you can simply trigger it. 如果您的问题是您希望此逻辑在页面加载和更改时运行,那么您只需触发它即可。

$(...).on('change', ...).trigger('change');

trigger will cause the event handler to execute immediately. trigger将导致事件处理程序立即执行。

You can simply add 你可以简单地添加

style="display: none;"

into the tag which has the github_link id when you define it in the HTML. 在HTML中定义它时,在具有github_link id的标记中。 Showing it and hiding it will work. 显示它并隐藏它将起作用。

It is more elegant though to define a CSS class like: 虽然定义一个CSS class更为优雅:

.invisible {
    display: none;
}

and use addClass / removeClass to hide/show the tag, since, as far as I know, show() and hide() are not the fastest features in jQuery: 并使用addClass / removeClass隐藏/显示标记,因为据我所知, show()hide()不是jQuery中最快的功能:

$('select').on('change', function() {
      var developer = {{ developer_id }};
      var value = $(this).val();
      console.log(developer);

      $("#github_link")[((value == developer) ? "remove" : "add") + "Class"]("invisible");
})

your script loads when your select being changed, so to make it works when the page loads, you need to have it outside any event 当您的选择被更改时,您的脚本会加载,因此要在页面加载时使其工作,您需要将其置于任何事件之外

var developer = {{ developer_id }};

$('select').on('change', function() {
  var value = $(this).val();
  console.log(developer);

  if (value == developer) {
    $('#github_link').show();
  } else {
    $('#github_link').hide();
  }
})

// Outside any events as well

var value = $('select').val();

if (value == developer) {
    $('#github_link').show();
} else {
    $('#github_link').hide();
}

or to make it simpler, we can have it inside a function: 或者为了使它更简单,我们可以将它放在一个函数中:

var developer = {{ developer_id }};

function fName(value){
    if (value == developer) {
         $('#github_link').show();
    } else {
        $('#github_link').hide();
    }
}

$('select').on('change', function() {
    var value = $(this).val();
    console.log(developer);

    fName(value)
});
fName($('select').val());

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

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