2016-03-27 37 views
1

文書の準備中にselect要素オプションのイベントを発生させたい次のコードを使用しますが、未定義の値option:seletedを返します。jqueryの要素idを選択する

$("option:selected").ready(function(){ 
    alert($(this).val());//use to get seleted option value 
    alert($(this).parent().val());//use to get selected option value using select 
    alert($(this).parent().attr("id"));//use to get select element id of selected:option 
}); 

その弾力はjQueryを使って

+0

早く助けてください – amir

答えて

1

を未定義のたびに

$(document).ready(function() { // you test if document is ready 
    if ($("option:selected").length > 0) { 
    // your code 
    alert($("option:selected").val());//use to get seleted option value 
    alert($("option:selected").parent().val());//use to get selected option value using select 
    alert($("option:selected").parent().attr("id"));//use to get select element id of selected:option 
    } 
}); 

See this fiddle