使用jQuery获取组合框的选定文本,按组合框的名称?

时间:2012-01-13 11:47:05

标签: jquery combobox

如何使用jQuery从组合框中获取选定的文本值。

我只有组合框的“名称”。

所以,我想要所选项目的文字,使用组合框的名称,而不是ID。

我有,

var selected_fld = ( $(this).attr('name') );

我该如何进一步处理?

5 个答案:

答案 0 :(得分:12)

$('select[name=nameOfTheBox]').val();

$('select[name=nameOfTheBox] option:selected').val();

将为您提供所选选项的值

$('select[name=nameOfTheBox] option:selected').text();

将为您提供文字

答案 1 :(得分:8)

这可以通过以下方式完成,以获得实际的文本值...

var value = $("[name='MyName'] option:selected").text();

或者这样可以获得选项'value'属性......

var value = $("[name='MyName']").val();

使用以下html,第一个会给你'MyText',第二个会给你'MyValue'

<select name="MyName">
   <option value="MyValue" selected="selected">MyText</option>
</select>

Here is a working example

答案 2 :(得分:1)

尝试以下

<select name="name1">
      <option value="val1">val1</option>
      <option value = "val2">val2</option>
</select>

var text = $("select[name='name1'] option:selected").text();

答案 3 :(得分:0)

<select id ="myCombo">
   <option value="Play selected="selected">Here</option>
   <option value="Once">Again</option>
</select>

$('#myCombo option:selected').text();

这将返回你&#34; here&#34;

$('myCombo option:selected').val();

这将返回你&#34;播放&#34;

答案 4 :(得分:0)

    <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery-3.1.0.js"></script>
    <script>
        $(function () {
            $('#selectnumber').change(function(){
                alert('.val() = ' + $('#selectnumber').val() + '  AND  html() = ' + $('#selectnumber option:selected').html() + '  AND .text() = ' + $('#selectnumber option:selected').text());
            })
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <select id="selectnumber">
            <option value="1">one</option>
            <option value="2">two</option>
            <option value="3">three</option>
            <option value="4">four</option>
        </select>

    </div>
    </form>
</body>
</html>

Click to See OutPut Screen

谢谢...:)

相关问题