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

我想要<select>元素的onchange事件顯示警報,但它不起作用。onchange select run function

任何想法有什麼不對?

+0

它確實對我有效。 – bfavaretto

+0

它的確如此:http://jsfiddle.net/H4kuk/。 – pimvdb

+0

[Works](http://jsfiddle.net/cggdc/)對我來說很好.. – Prisoner

回答

1

這應該工作,但你也可以試試這個前提是你有你的頁面

$(function(){ 

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

用於提示jQuery的+1,請記住包含它! –

2

我假設你的問題是,alert()呼叫顯示不確定代替<select>的價值上jQuery

如果你想看到<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

如果你想讓它工作,你應該在你的警報功能中給出一個字符串作爲參數...

相關問題