jQuery select()方法不起作用

时间:2015-07-22 13:16:43

标签: javascript jquery

我需要获取select标签中所有选定选项的数据,为此我使用了onclick()函数,它只提供了单击选项的数据。但是如果用户选择CTRL * A的所有选项,则不会收到任何数据。我试过使用在这种情况下不起作用的select()。



<?xml version="1.0" encoding="utf-8"?>

<configuration>
  <configSections>
    <section name="oracle.manageddataaccess.client"
             type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
  </configSections>
  <system.data>
    <DbProviderFactories>
      <remove invariant="Oracle.ManagedDataAccess.Client" />
      <add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client"
           description="Oracle Data Provider for .NET, Managed Driver"
           type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
    </DbProviderFactories>
  </system.data>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <publisherPolicy apply="no" />
        <assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral" />
        <bindingRedirect oldVersion="4.121.0.0 - 4.65535.65535.65535" newVersion="4.121.2.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

  <oracle.manageddataaccess.client>
    <version number="*">
      <dataSources>
        <dataSource alias="MyDataSource"
                    descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=100.100.100.100)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=myservice.com)))" />
      </dataSources>
    </version>
  </oracle.manageddataaccess.client>
</configuration>
&#13;
//jQuery onclick()
$('select[name=sensors]').on('click', function(){
    $('#demo').text($('select[name=sensors]').val());
});

//jQuery select()
$('select[name=sensors]').select(function(){
    $('#demo2').text($('select[name=sensors]').val());
});
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:5)

不要绑定click,而是绑定change。这样就可以考虑来自其他类型互动的变化:

$('select[name=sensors]').on('change', function(){
    $('#demo').text($('select[name=sensors]').val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select type='list' name='sensors' multiple>
  <option value= "e11">e11</option>
  <option value= "e12">e12</option>
  <option value= "e13">e13</option>
  <option value= "e14">e14</option>
</select>
<div id="demo"></div>

关于select的实验,以下是the documentation所说的内容:

  

当用户发出文本时,select事件将发送到元素   在里面选择。此活动仅限于   田野和盒子。

这里没有相关性,因为用户没有选择 text 而是选项。

答案 1 :(得分:1)

这样做

$('select[name=sensors]').change(function(){
    $('#demo2').text($(this).val());
});