2012-06-28 135 views
-1

所有, 選擇的名稱/ ID,我有以下選擇:獲取已更改

<select name="options_1" id="options_1" class="select_me"> 
    <option>1</option> 
    <option>2</option> 
</select> 

我有我的網頁上同一類中的多個選擇。當我改變這些選擇中的一個時,我想獲得select的名稱或ID,然後是下劃線之後的值。

任何人都可以讓我知道如何做到這一點?謝謝!

+0

[你嘗試過什麼?](HTTP:/ /whathaveyoutried.com) – madfriend

回答

2
$(document).ready(function() { 
    $("SELECT[id^=options]").bind('change', function() { 
     var fullId = $(this).attr('id'); 
     var idParts = fullId.split('_'); 
//  idParts[0] would be options 
//  idParts[1] would be 1 
    }); 
}); 
0

使用類作爲選擇的變化()處理

DEMO:http://jsfiddle.net/QjEkS/

$('select.select_me').change(function(){ 
    var id=this.id; 
    var num_val= id.split('_').pop(); 
    alert('ID: '+id +', Num: '+num_val);  
}); 
0
$('.select_me').on('change', function(){ 
    var id = this.id, // original id 
     number = id.substr(id.indexOf('_') + 1); // numeric part of id 
}); 

Working sample

相關問題