2011-08-15 15 views
2
<select onchange="alert();"> 
      <option>d</option> 
      <option>de</option> 
      <option>dewe</option> 
      <option>dewee</option> 
     </select> 

を選択れるonchange私は警告を表示する<select>要素ののonchangeイベントをしたいが、それは働いていません。が実行機能

何か問題がありますか?

+0

これは機能します。 – bfavaretto

+0

それは:http://jsfiddle.net/H4kuk/です。 – pimvdb

+0

[作品](http://jsfiddle.net/cggdc/)私にとっては.. – Prisoner

答えて

1
動作するはず

しかし、あなたはまた、あなたがjQuery

ページ上
$(function(){ 

    $("select").change(function(){ 
     alert($(this).val()); 
    }); 
}); 
+1

+1 jQueryを提案するために、それを含めることを忘れないでください! –

2

を含めて、これを試すことができます私はあなたの問題がalert()呼び出しが未定義代わりの<select>の値を表示することであると仮定しています。

あなたが<select>の値を表示したい場合は、代わりに次の操作を行います。

<select onchange="alert(this.value);"> 
    <option>d</option> 
    <option>de</option> 
    <option>dewe</option> 
    <option>dewee</option> 
</select> 

jsfiddle example


それとも、あなたが実際にjQueryを使用している場合、あなたはこのようにそれを行うことができました:

//This is an anonymous function that calls itself with the jQuery object as an argument 
//This allows you to use the `$` when making jQuery calls but the code 
//will run without modification if jQuery is in noConflict() mode 
(function($) { 
    //This is the important part 
    //Here, we bind to the change event and alert the value 
    $('select').change(function(e) { 
     alert($(this).val()); 
    }); 
})(jQuery) 

jsfiddle example 2

0

アラート機能では、文字列をパラメータとして指定する必要があります。

関連する問題