2013-06-21 124 views
2

嘿,我想獲取我的下拉列表中的值或選定的索引在jquery中。當我嘗試它時,它會回到未定義狀態。當我嘗試.index()它只給我索引而不是選定的索引。JQuery的下拉列表值返回undefined

<td> 
    <asp:DropDownList id="ddlFullName" runat="server" Width="48px" /> 
</td> 

這裏是我的Jquery。

$(document).ready(function() { 

     $("select[id$='_ddlFullName']").change(function() { 

      console.log("changed"); 
      var selectedText = $("select[id$='_dllFullName'] option:selected").val(); 
      console.log("selected text= " + selectedText);    
     }); 

    }); 

另一件事。我在客戶端使用DotNetNuke,它會將我的下拉列表包裝到鏈接中,並顯示所選值或文本中顯示的文本,如下所示。

<a href="javascript:void(0)" id="Skinneddnn_ctr481_ViewResellerSite_Dashboard_ResellerSite/BulkMailingProcessingMain_ddlFullName" unselectable="on" tabindex="-1" class="rfdSelect rfdSelect_Growll" style="width: 44px;"><span class="rfdSelectOuter"><span class="rfdSelectText">c_county</span></span></a> 

與低於

<select name="dnn$ctr481$ViewResellerSite$Dashboard$ResellerSite/BulkMailingProcessingMain$ddlFullName" id="dnn_ctr481_ViewResellerSite_Dashboard_ResellerSite/BulkMailingProcessingMain_ddlFullName" style="width: 44px;" class="rfdRealInput" _rfddecoratedid="Skinneddnn_ctr481_ViewResellerSite_Dashboard_ResellerSite/BulkMailingProcessingMain_ddlFullName"> 
       <option value=""></option> 
       <option value="c_add1">c_add1</option> 
       <option value="c_city">c_city</option> 
       <option value="c_state">c_state</option> 
       <option value="c_zip">c_zip</option> 
       <option value="c_key">c_key</option> 
       <option value="c_f_nm">c_f_nm</option> 
       <option value="c_l_nm">c_l_nm</option> 
       <option value="c_add2">c_add2</option> 
       <option value="c_county">c_county</option> 
       <option value="c_country">c_country</option> 
       <option value="c_phone">c_phone</option> 
       <option value="c_company">c_company</option> 
       <option value="c_fax">c_fax</option> 
       <option value="c_pager">c_pager</option> 
       <option value="c_cellular">c_cellular</option> 
       <option value="c_em">c_em</option> 
       <option value="pw">pw</option> 
       <option value="username">username</option> 
       <option value="gender">gender</option> 
       <option value="opt1">opt1</option> 
       <option value="s_f_nm">s_f_nm</option> 
       <option value="s_l_nm">s_l_nm</option> 
       <option value="s_add1">s_add1</option> 
       <option value="s_add2">s_add2</option> 
       <option value="s_city">s_city</option> 
       <option value="s_state">s_state</option> 
       <option value="s_county">s_county</option> 
       <option value="s_zip">s_zip</option> 
       <option value="s_country">s_country</option> 
       <option value="s_phone">s_phone</option> 
       <option value="s_fax">s_fax</option> 
       <option value="s_company">s_company</option> 
       <option value="s_pager">s_pager</option> 
       <option value="s_cellular">s_cellular</option> 
       <option value="cg_id">cg_id</option> 
       <option value="opt3">opt3</option> 
       <option value="opt2">opt2</option> 
       <option value="ibo">ibo</option> 
       <option value="sibo">sibo</option> 
       <option value="libo">libo</option> 
       <option value="plevel">plevel</option> 
       <option value="var_num">var_num</option> 
       <option value="var_name">var_name</option> 
       <option value="var_type">var_type</option> 
       <option value="var_ref">var_ref</option> 
       <option value="pay_method">pay_method</option> 
       <option value="sales_exempt">sales_exempt</option> 
       <option value="sacct_code">sacct_code</option> 
       <option value="cctype">cctype</option> 
       <option value="logon_redirect">logon_redirect</option> 
       <option value="logon_redirect_pagename">logon_redirect_pagename</option> 
       <option value="mgr_account_access">mgr_account_access</option> 
       <option value="mgr_managerfront_access">mgr_managerfront_access</option> 
       <option value="type">type</option> 
       <option value="s_flospath">s_flospath</option> 
       <option value="flospath">flospath</option> 
       <option value="blospath">blospath</option> 
       <option value="s_blospath">s_blospath</option> 
       <option value="bsponsor">bsponsor</option> 
       <option value="fsponsor">fsponsor</option> 
       <option value="flospath_level">flospath_level</option> 
       <option value="blospath_level">blospath_level</option> 
       <option value="spouse_f_nm">spouse_f_nm</option> 
       <option value="spouse_l_nm">spouse_l_nm</option> 
       <option value="bus_type">bus_type</option> 
       <option value="emdate">emdate</option> 
       <option value="leglock">leglock</option> 
       <option value="leg_strength">leg_strength</option> 
       <option value="bleg">bleg</option> 
       <option value="bactivated">bactivated</option> 
       <option value="blmvol">blmvol</option> 
       <option value="bqualfied">bqualfied</option> 
       <option value="flag1">flag1</option> 
       <option value="flag2">flag2</option> 
       <option value="flag4">flag4</option> 
       <option value="flag5">flag5</option> 
       <option value="flag3">flag3</option> 
       <option value="NotifyFlag">NotifyFlag</option> 
       <option value="CustomerPath">CustomerPath</option> 
       <option value="CustFlags1">CustFlags1</option> 
       <option value="cust_ser_logon">cust_ser_logon</option> 
       <option value="opt4">opt4</option> 
       <option value="opt5">opt5</option> 
       <option value="opt6">opt6</option> 
       <option value="opt7">opt7</option> 
       <option value="opt8">opt8</option> 
       <option value="opt9">opt9</option> 
       <option value="opt10">opt10</option> 
       <option value="opt11">opt11</option> 
       <option value="opt12">opt12</option> 
       <option value="opt13">opt13</option> 
       <option value="opt14">opt14</option> 
       <option value="opt15">opt15</option> 
       <option value="features">features</option> 
       <option value="bleg1">bleg1</option> 
       <option value="row_added">row_added</option> 
       <option value="status">status</option> 
       <option value="ws_nm">ws_nm</option> 
       <option value="date">date</option> 

      </select> 

回答

1

實際下拉列表試試這個,

$(function(){ 
    $("select#ddlFullName").change(function() { 
      console.log("changed"); 
      var selectedText = $(this).val(); 
      console.log("selected text= " + selectedText);    
    }); 
}); 
+0

該做的,我需要改變我的客戶ID模式靜態? – Mitch

+0

真棒,這工作。謝謝Rohan! – Mitch

1

DEMO - >http://jsfiddle.net/3AFwj/

正如你所申請的類,那麼使用Class而不是ID

$(".rfdRealInput").change(function() { 
     console.log("changed"); 
     var selectedText = $(".rfdRealInput option:selected").val(); 
     console.log("selected text= " + selectedText);    
    }); 
0
Try using selector like this: 

$(document).ready(function() { 

    $("#ddlFullName").change(function() { 

     console.log("changed"); 
     var selectedText = $("#dllFullName").val(); 
     console.log("selected text= " + selectedText);    
    }); 

}); 
+0

在服務器端id將被更改。 –