Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
選択リストボックスがあり、ページをロードするときにリスト値の 1 つを選択したいと考えています。私は次のアプローチを使用していますが、うまくいきます:
$("#mySelect").find("option[value=3]").attr("selected",true);
3 定数を var に置き換える最良の方法は何でしょうか? 例えば:
var myVar = 3;
簡単なはずですが、わかりません。
セレクターを連結することで、変数をセレクターに入れることができます。
ライブデモ
var myVar = 3; $("#mySelect").find("option[value="+myVar+"]").attr("selected",true);