最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - correct way to get selected option in jquery from an object - Stack Overflow

programmeradmin4浏览0评论

I've seen on StackOverflow and googling around that the most used way to get the selected text from a <SELECT> element with jquery is like this

$("#cboId :selected").text()

what I have is not the id, but an object. I have an object, say

var myCombo= $("#cboId");

ing from an earlier piece of code.

I've done like this to get the selected text, but is ugly:

  $("#" + myCombo.attr('id') + " :selected").text() ;

Is there any cleaner way to do it?

I've seen on StackOverflow and googling around that the most used way to get the selected text from a <SELECT> element with jquery is like this

$("#cboId :selected").text()

what I have is not the id, but an object. I have an object, say

var myCombo= $("#cboId");

ing from an earlier piece of code.

I've done like this to get the selected text, but is ugly:

  $("#" + myCombo.attr('id') + " :selected").text() ;

Is there any cleaner way to do it?

Share Improve this question edited Sep 7, 2022 at 23:35 Daniel Widdis 9,13113 gold badges48 silver badges68 bronze badges asked Jun 10, 2009 at 9:16 pomarcpomarc 2,2244 gold badges25 silver badges30 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 18
var selectedText = $(":selected",myCombo).text();
发布评论

评论列表(0)

  1. 暂无评论