admin管理员组

文章数量:1289542

I am trying to get the selected value from an event.

I don't know which method to get this value. Any suggestions ?

//update item quantity
cartList.on('change', 'select', function (event) {
   var row = $(event.target).parents('.product');
   updateItemQuantity(row.data('row_id'), QUANTITY_IS_SELECTED_VALUE);
   quickUpdateCart();
});

I am trying to get the selected value from an event.

I don't know which method to get this value. Any suggestions ?

//update item quantity
cartList.on('change', 'select', function (event) {
   var row = $(event.target).parents('.product');
   updateItemQuantity(row.data('row_id'), QUANTITY_IS_SELECTED_VALUE);
   quickUpdateCart();
});
Share Improve this question edited Sep 21, 2019 at 10:53 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Dec 28, 2018 at 23:07 Ahmad ElkenanyAhmad Elkenany 5851 gold badge5 silver badges23 bronze badges 3
  • 3 event.target.value? – CertainPerformance Commented Dec 28, 2018 at 23:08
  • Possible duplicate of Get selected value in dropdown list using JavaScript? – Heretic Monkey Commented Dec 29, 2018 at 4:09
  • yes, i just realized ( event.target is the element that triggered the event ) – Ahmad Elkenany Commented Dec 29, 2018 at 9:36
Add a ment  | 

5 Answers 5

Reset to default 4

event.target is the element that triggered the event. You can read its value using jQuery by doing $(event.target).val(). You can also use the this keyword, which holds the same element ($(this).val()).

Instead of using jQuery, you could also retrieve the value through the vanilla DOM method of this.options[this.selectedIndex].text.

try this

event.target.options[event.target.options.selectedIndex].text

The .val() method in jQuery returns or sets the value attribute of the selected elements.This method returns the value of the value attribute of the FIRST matched element.

 $(selector).off('change').on('change', function () {
    var selectedValue = $(this).val();
    alert(selectedValue);
    //Your other logic here
  });

With pure javascript you can find selected option and all related information:

console.log(event.target.selectedOptions[0].value);
console.log(event.target.selectedOptions[0].label);

you could target the Select by class, then add an onChange handler, and get the value

var selectItem = document.querySelector("select.cartList");
selectItem.addEventListener("change", function(event) {
    var newChangedValue = event.target.value;
    console.log(newChangedValue);
});

本文标签: jqueryHow to get a selected value from a javascript ( event ) objectStack Overflow