2011-09-10 32 views
0

私はこれを動作させることができないようで、私は間違いを見つけることができません。選択肢は次のとおりです。Jquery #divオプション:選択した問題

<select id="elecspend"> 
     <option selected="selected" value="month">Monthly Spend</option> 
     <option value="yearly">Yearly Spend</option> 
     <option value="quarterly">Quarterly Spend</option> 
     <option value="consumption">Yearly Consumption (kWh)</option> 
    </select> 
<div class="consumptext">Enter your monthly spend in here: </div> <input style="width: 80px;" type="text"> 

ここはjqueryです。

$(document).ready(function() { 
$("#elecspend").change(function() { 


    if($("#elecspend option:selected").attr('value') == 'monthly') { 
     $(".consumptext").html("Please enter your monthly electricity spend"); 
    } 
}); 
}); 

ありがとうございました。 サム

+0

目的の機能が記述されていません。 –

答えて

2

あなたのオプションに値 "月間"はありません。私はあなたが "月"を望むと思うし、this.valueを使って値を得ることができます。これに

if($("#elecspend option:selected").attr('value') == 'monthly') 

:これを変更してみてください

if (this.value == 'month') 

それはここで働いています:http://jsfiddle.net/jfriend00/tFvtb/

+0

attr( 'value')とval()の違いは何ですか?ああ、お時間をありがとう。 –

+0

'.attr(" value ")'はこの場合はうまくいくはずですが、既にjQueryを使用している場合は、 '.val()'がよりクリーンで、jQueryコードにいくつかのクロスブラウザバグのクリーンアップがある'.val()'のために。おそらく大きな違いはありません。 – jfriend00

+0

私は 'this.value'を使うだけで簡単にそれを実現しました。 – jfriend00

0

「月間」と「月間」を混同しています。 「毎月」に月エントリの値を変更してから、このコードを使用します。

$("#elecspend").change(alterPrompt); 

function alterPrompt() { 
    var selectedValue = $("#elecspend option:selected").attr('value'); 
    $(".consumptext").html("Please enter your "+ selectedValue +" electricity spend"); 
} 
alterPrompt(); 
+0

Bernhardありがとうございます。 –

0

代わり

if($("#elecspend option:selected").attr('value') == 'monthly') 

if ($("#elecspend").val() == "month") 

を行うことができますvalのAPIドキュメントを参照してください。ここにはjsFiddle exampleがあります。

0

あなたはちょうどthis.valueを使用できますか?

将来の先端は、あなたが出回っあなただけ使用することができ、あなたの変更に#elecspendを発見した:

$("option:selected", this) 

魔女が速いオプションです。

関連する問題