简体   繁体   English

在 select 更改时,获取选项值

[英]On select change, get option value

I have seen numerous questions on this which give similar answers, namely these:我已经看到了很多关于此的问题,它们给出了类似的答案,即:

However, when following the approaches they are, it does nothing for me?但是,当遵循它们的方法时,它对我没有任何帮助吗?

 $(function() { $('.blogFilter__form').on('change', function(){ // location.href = $(this).val(); console.log($(this).val()); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form class="blogFilter__form"> <select class="blogFilter__select"> <option class="blogFilter__option" value="">Topics</option> <option class="blogFilter__option" value="/topics/how-to/">How to</option> <option class="blogFilter__option" value="/topics/news/">News</option> </select> </form>

What I'm trying to do is redirect user based on what option value is selected.我要做的是根据选择的option值重定向用户。

Currently, running console.log($(this).val());目前,正在运行console.log($(this).val()); logs nothing for me?为我记录什么?

It is because you have the change event listener on the form itself rather than on the select.这是因为您在表单本身而不是 select 上有更改事件侦听器。 If you change the function to listen for the .blogFilter__select being changed it should work:如果您更改 function 以侦听正在更改的.blogFilter__select它应该可以工作:

$(function() {
  $('.blogFilter__select').on('change', function(){
    // location.href = $(this).val();
    console.log($(this).val());
  });
});

Previously you were logging the 'value' of the form, which is empty, rather than the value of the select.以前您记录的是表单的“值”,它是空的,而不是 select 的值。

Just a further, somewhat unrelated point.只是更进一步,有点不相关的一点。 You should probably use id instead of class for this type of function.对于这种类型的 function,您可能应该使用 id 而不是 class。 The reason I say this, is that if you have multiple selects in the same form.我这样说的原因是,如果您在同一个表单中有多个选择。 You can use class="blogFilter__select" to style them, but if you want to listen for a change like this, it may not behave as you intend it to.您可以使用class="blogFilter__select"来设置它们的样式,但是如果您想监听这样的更改,它可能不会像您想要的那样运行。

I have also added an additional way you could log the select value when the form changes, if that is what you wanted.我还添加了另一种方法,您可以在表单更改时记录 select 值,如果这是您想要的。 (It is commented out though so uncomment it and play around with it if you want to use it) (它被注释掉了,所以如果你想使用它,请取消注释并玩弄它)

 // Function only listens on pageSelect // (use # instead of. to choose id instead of class) // test with.blogFilter__select and you'll see it logs both $(function() { $('#blogFilter__pageSelect').on('change', function() { console.log($(this).val()); }); }); // Alternatively, if you wanted to listen for whenever the form was changed, but log the value of the select, you can do it by using the id of the select instead of 'this': // $(function() { // $('#blogFilter__form').on('change', function() { // console.log($('#blogFilter__pageSelect').val()); // }); // });
 /* style affects both <select>s*/.blogFilter__select { display: block; margin-bottom: 1em; color: red; font-weight: bold; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="blogFilter__form" class="blogFilter__form"> <select id="blogFilter__pageSelect" class="blogFilter__select"> <option class="blogFilter__option" value="">Topics</option> <option class="blogFilter__option" value="/topics/how-to/">How to</option> <option class="blogFilter__option" value="/topics/news/">News</option> </select> <select id="blogFilter__secondSelect" class="blogFilter__select"> <option class="blogFilter__option" value="1">One</option> <option class="blogFilter__option" value="2">Two</option> <option class="blogFilter__option" value="3">Three</option> </select> </form>

If you log $this, you can see that it's the blogfilter__form which has no value.如果您记录 $this,您可以看到它是没有任何价值的 blogfilter__form。 Use the event to get the target value instead.改为使用事件来获取目标值。

 $(function() { $('.blogFilter__form').on('change', function(e){ console.log(e.target.value); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form class="blogFilter__form"> <select class="blogFilter__select"> <option class="blogFilter__option" value="">Topics</option> <option class="blogFilter__option" value="/topics/how-to/">How to</option> <option class="blogFilter__option" value="/topics/news/">News</option> </select> </form>

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

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