2015-04-17 63 views
2

我是一個初學者,我即將完成我的第一個網站。作爲jQuery中的事件處理程序,不能調用按鈕

但我只是不能得到最後一個按鈕的工作。它是最簡單的任務,但這只是不工作。

$(document).ready(function() { 
 
    alert("mailto script loaded!"); 
 
}) 
 

 
//eventhandler send-button 
 
$('#send').onclick(function() { 
 
    alert("Handler has beend called."); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="buttonsend"> 
 
    <button class="button button-pill button-flat-action" id="send">Senden</button> 
 
</div>

你們可以給我一個提示問題出在哪裏? 非常感謝您提前。我搜索了一切,但基本上沒有辦法基於我需要做的事情。我只是不知道我要去哪裏。

+3

將處理程序代碼添加到文檔就緒。把'onclick'改成'click' –

回答

2

jQuery事件被稱爲click而不是onclick。更改爲:

$('#send').click(function() { 

根據其中的JavaScript被放置在頁面上,你可能需要將其包裝在DOM準備好處理程序。

$(document).ready(function() { 
    $('#send').click(function() { 
     alert("Handler has beend called."); 
    }); 
}); 
+0

非常感謝! – Dyon

2

除了把處理器的文件準備,你應該解決您的語法了。您需要使用.on()來處理該方法的第一個參數。文檔瀏覽:https://api.jquery.com/on/

//alert if the js-script is succesfully loaded 
$(document).ready(function() { 
    alert("mailto script loaded!"); 
    //eventhandler send-button 
    $('#send').on('click',function() { 
     alert("Handler has beend called."); 
    }); 
}) 

工作實例: http://jsfiddle.net/5xn5tpq4/

在JavaScript窗格中的代碼已經被包裹着準備處理程序。

相關問題