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
5 Answers
Reset to default 4event.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
版权声明:本文标题:jquery - How to get a selected value from a javascript ( event ) object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741421491a2377830.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论