2011-07-08 73 views
1

有沒有辦法將點擊事件綁定到選擇選項而不是選擇框本身?Jquery綁定點擊選擇選項,但不是選擇元素本身

更改事件也不會爲我工作,因爲當我第一次點擊框我無法選擇第一個選項(因爲它已被默認選擇),我真的需要能夠也選擇第一個選項。

有沒有辦法解決這個問題,要麼綁定點擊或更改事件?

+2

添加一個 '請選擇...' 選項或運行在頁面加載變化碼? – Town

+2

另一個扳手扔在這裏...用戶可以選擇框,並選擇,而無需使用「點擊」事件。 – kasdega

+0

你試過'$('option')。click(function(){// ..這裏有一些代碼});'? – Trey

回答

4

你既可以:

一)select添加 '請選擇...' option作爲默認項。

B)運行「選擇改變」在頁面加載代碼拿起默認情況下,something like this

<select> 
    <option>one</option> 
    <option>two</option> 
    <option>three</option> 
</select> 

Current Value: <span></span> 

// call the changed function on document ready 
// to get the 'default' value 
$(function() { 
    selectionChanged(); 
}); 

// hook up the change event of the select box 
$('select').change(selectionChanged); 

// event handler for the change event 
function selectionChanged(e) { 
    $('span').html($('select').val()); 
} 
+0

+1選項b) –

+0

是啊選項b將爲我工作,謝謝城市! – mfreitas

+0

@mfreitas:我的快樂伴侶:) – Town