我的印象是,我可以通过执行这个$(this).val()来获取一个选择输入的值;并将onchange参数应用到select字段。

只有在引用ID时,它才会起作用。

我怎么用这个。


当前回答

只使用JS

 let select=document.querySelectorAll('select') 
  select.forEach(function(el) {
    el.onchange =  function(){
     alert(this.value);
      
  }}
  )

其他回答

对于所有选择,调用此函数。

$('select').on('change', function()
{
    alert( this.value );
});

只有一个选择:

$('#select_id') 

jQuery获取在Change事件上使用select html元素的值

演示和更多的例子

$(document).ready(function () { $('body').on('change','#select_box', function() { $('#show_only').val(this.value); }); }); <!DOCTYPE html> <html> <title>jQuery Select OnChnage Method</title> <head> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> </head> <body> <select id="select_box"> <option value="">Select One</option> <option value="One">One</option> <option value="Two">Two</option> <option value="Three">Three</option> <option value="Four">Four</option> <option value="Five">Five</option> </select> <br><br> <input type="text" id="show_only" disabled=""> </body> </html>

我有一个例子来帮助新想法。 我希望这对任何人都有帮助。

$('#editpricelist_box').change(function ($e){
    $('input[name="edit_unit_price"]').val(parseFloat(this.val()).toFixed(2));
    console.log(this.val());
});
$('#select-id').change(function() {
    console.log($(this).val());
});

这对我来说很管用。我尝试了其他所有方法,但都没有成功:

<html>

  <head>
    <title>Example: Change event on a select</title>

    <script type="text/javascript">

      function changeEventHandler(event) {
        alert('You like ' + event.target.value + ' ice cream.');
      }

    </script>

  </head>

  <body>
    <label>Choose an ice cream flavor: </label>
    <select size="1" onchange="changeEventHandler(event);">
      <option>chocolate</option>
      <option>strawberry</option>
      <option>vanilla</option>
    </select>
  </body>

</html>

摘自Mozilla