3

I'm getting a return value for this query that doesn't make sense.

code is:

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

result:

10   Activity

can anyone tell me why I'm getting back a 10 and spaces and how to get rid of them?

html:

<select name="datagrid_filter" onchange="startFilter(this);">
<option>All</option>
<option disabled="true">Assignment:</option>
<option disabled="true">Client:</option>
<option disabled="true">Type:</option>
    <option class="type" value="Activity"> &nbsp Activity</option>
    <option class="type" value="Alert"> &nbsp Alert</option>
    <option class="type" value="Lead"> &nbsp Lead</option>
    <option class="type" value="Notification"> &nbsp Notification</option>
</select>
4

2 回答 2

1

Working Demo Here

You can remove unwanted spaces using the the function:

jQuery.trim( str )

regarding the "10" if you have other <select> the page, to avoid problems, refer directly to the <select name="datagrid_filter"> by name

$("select[name='datagrid_filter'] option:selected").text()
于 2013-09-21T18:36:18.720 回答
-1

.text() 返回文本值,它是一个字符串。您可以将 parseInt() 应用于它,或者寻找 .val()

例如。

var option = parseInt( $(":selected").text(), 10 );

或者

 var option = $(":selected").val()
于 2013-09-21T15:21:53.507 回答