Jquery obtains the selected text and value. jquery obtains the selected text.
1. Description
(1) obtain the selected index in the select drop-down box.
$("#selection").get(0).selectedIndex;
(2) obtain the selected value from the select drop-down box.
$("#selection option:selected").val();
(3) obtain the selected text from the select drop-down box.
$("#selection option:selected").text();
2. Source Code
Html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <HTML xmlns =" http://www.w3.org/1999/xhtml "> <HEAD> <script type =" text/javascript "src = ".. /website/unnamed Site 2/jquery-1.11.0.js "> </script> <script type =" text/javascript "> $ (function () {$ (" # btn "). click (function () {// obtain the select drop-down box index var index =$ ("# selection "). get (0 ). selectedIndex; // obtain the value of the select drop-down box var selectVal =$ ("# selection option: selected "). val (); // obtain the select drop-down box text var selectText =$ ("# selection option: selected "). text (); alert ("Get select drop-down box index:" + index + "\ n" + "Get the select drop-down box value: "+ selectVal +" \ n "+" Get the select drop-down box text: "+ selectText );});}); </script> <DIV id = select_val> <SELECT id = selection> <OPTION selected value = 0> carp </OPTION> <OPTION value = 1> catfish </OPTION> <OPTION value = 2> catfish </OPTION> <OPTION value = 3> mackerel </OPTION> <OPTION value = 4> ainemo </OPTION> <OPTION value = 5> hongjin luo Han Yu </OPTION> <OPTION value = 6> Luo Han Yu, Rainbow King </OPTION> <OPTION value = 7> taijin Luo Han Yu </OPTION> </SELECT> </DIV> <INPUT id = btn value = get the selected text and value type = button>
3. Implementation result
(1) Select the first item
(2) Select the last item
How does jquery obtain the selected text content in the select element? (Rather than the option value) How to Write
Try this one. I tried it just now. Yes.
Var tt = $ ("form select [name = selectName]"). find ('option: selected'). text ();
Alert (tt );
Replace selectName with the name value of the drop-down box in your form.
You can use ID to query the data.
Var tt = $ ("# selectId"). find ('option: selected'). text ();
Jquery obtains the selected content in the text box.
<Script> function GetSelectedText () {var selText = ""; if (window. getSelection) {// all browsers, before t IE before version 9 if (document. activeElement & (document. activeElement. tagName. toLowerCase () = "textarea" | document. activeElement. tagName. toLowerCase () = "input") {var text = document. activeElement. value; selText = text. substring (document. activeElement. selectionStart, document. activeEl Ement. selectionEnd);} else {var selRange = window. getSelection (); selText = selRange. toString () ;}} else {if (document. selection. createRange) {// Internet Explorer var range = document. selection. createRange (); selText = range. text ;}} return selText;} function OnSelectInput (input) {selText = GetSelectedText (); alert ("The contents of the current selection are \ n" + selText );} </script> <input si Ze = "40" value = "Select a part of this text! "Onselect =" OnSelectInput (this) "> Source: help.dottoro.com/ljmwkbaj.php