简体   繁体   English

如何在 jquery 中获取下拉所选项目的名称?

[英]How to get name of the dropdown selected item in jquery?

If i have the bookd_ids and book_titles as follows in the database:如果我在数据库中有 bookd_ids 和 book_titles 如下:

 id   |  title
 ---------------
 1    |  abc
 2    |  xyz
 3    |  pqr

I have the books dropdownlist as show below:我有如下所示的书籍下拉列表:

<%=  select("books", "book_id", Book.all.collect {|b|
                [ b.title, b.id ] }, {:include_blank => 'Select Book'})%>

i can get the id of the books thru query this way:我可以通过以下方式获取书籍的 ID:

var id = $("#books_book_id").val();

How to get the abc, xyz pqr titles using jquery?如何使用 jquery 获得 abc、xyz pqr 标题?

var text = $("#books_book_id option:selected").text();

Have put some space between: selected之间留了一些空间:已selected

var value = jQuery("#edit-system-name :selected").text();

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

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