2016-09-20 11 views
1

jQueryを使用してドロップダウンリストで選択した値を読みたいとします。jQueryを使用してドロップダウン選択フィールドで選択した値を読み込みます

マイHTML:

<select id="selectValue"> 
    <option value="Yes">Yes</option> 
    <option value="No">No</option>       
</select> 

私のjQuery:

$(document).ready(function() { 
    var Value = $("#selectValue option:selected").text(); 
    alert(Value); 
}); 

誰もが私のコードが間違っているものを私に言うことはできますか?

$(document).ready(function() { 
    var Value = $("#selectValue").val(); 
    alert(Value); 
}); 
+0

.... – Rayon

+0

は '追加を持っているようだ;'あなた 'VARの値の末尾に= $ ( "#selectValue option:selected")。text();; 'ステートメント。そのコード以外は私のためにうまく動作します。このフィドルをチェックしてください:https://jsfiddle.net/vcq34tL2/ –

+2

@DavidR - それはどんな害も引き起こさないでしょう... – Rayon

答えて

0

です

それとも、jQueryのを拡張することができます。

微細加工

jQuery.fn.extend({ 
 
    valText: function() { 
 
    return jQuery('option[value="' + jQuery(this).val() + '"]', jQuery(this)).text(); 
 
    } 
 
}); 
 

 
$('button').click(function() { 
 
    console.log($('#selectValue').valText()); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<select id="selectValue"> 
 
    <option value="YesKey">Yes</option> 
 
    <option value="NoKey">No</option>       
 
</select> 
 

 
<button>get selected value</button>

0

ちょうどこの

$(document).ready(function() { 
var Value = $("#selectValue").find(":selected").text(); 
    alert(Value); 
}); 
0

を試してみてください。ここ はSNAPPのwithresultはval()方法を試してみてくださいdebbugerに
enter image description here

+0

こちらも同じですhttp://i.stack.imgur .com/WBb5o.png – Madhav

0

$('button').click(function() { 
 
\t var value = $('#selectValue').val(); 
 
\t var text = $('option[value="' + value + '"]', $('#selectValue')).text(); 
 
\t 
 
\t console.log(value); 
 
\t console.log(text); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<select id="selectValue"> 
 
    <option value="YesKey">Yes</option> 
 
    <option value="NoKey">No</option>       
 
</select> 
 

 
<button>get selected value</button>
0123を試してみてください。この

$('#yourDropdownId').change(function(){ 
    var selectedid=$('#yourDropdownId').val(); 
     alert(selectedid); 
    }); 
+0

これも動作していません。ここをクリックhttp://i.stack.imgur.com/WBb5o.png – Madhav

+0

私の作業コードスニペットで試してみてください:$( 'option [value = "' + val()+ '"]'、$( '#selectValue'))。 – Laurianti

+0

私の新しい例を読んでください、これは簡単になります – Laurianti

関連する問題