ドロップダウンを動的に作成します。
そのIDはddl1
、ddl2
、ddl3
とになります。
$('#ddl1').focus(function() {
var previous = this.value;
}).change(function() {
var index = $(this).find('selected').index();
$('#ddl1').find('option:eq(' + index + ')').hide();
$('#ddl2').find('option:eq(' + index + ')').hide();
});
$('#ddl2').focus(function() {
...
7つのドロップダウンを作成すると、7つのchange
イベントハンドラーを作成できます。
動的に変更イベントを作成する方法を実装しますか?
私のドロップダウン追加メソッドは次のとおりです。
var x=1;
var dropdown=('#ddl1').html();
$('#btnadd').click(function() {
$('#divname').append('<select id="ddl'+x+' > </select> <option>../option>');
x++;
});
すべてのドロップダウンに共通のクラスを与え、それを参照します。また、.on()
(イベント委任)を使用して、動的に作成された要素をキャッチします。
$(document).on('change', '.common-class', function() {
//Your function
var currentId = $(this).attr('id');
});
更新
select
要素を追加するたびに同じクラスを追加します。
//...
$('#divname').append('<select id="ddl'+ x + '" class="common-class"><option>../option></select>');
選択したオプション
選択したオプションを取得するには:
$('#current-id').find('option:selected').index();
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加