2015-11-01 228 views
3

我正在使用下面的代碼按鈕點擊事件使用jQuery。點擊按鈕時,頁面重新加載。如何停止頁面重新加載按鈕點擊jquery

$('#button1').click(function() { 
    //Code goes here 
    return false; 
}); 
+0

有一些代碼,明確增加了重裝上按鈕的功能,裏面的網頁點擊?據我所知,在一個簡單的按鈕點擊,頁面不會重新加載。 –

+0

我在頁面中添加了什麼 – Vicky

+0

只添加了jquery.min.js 1.11.3和我的代碼。 – Vicky

回答

0

您可以使用event.preventDefault()來防止發生默認事件(單擊)。

$('#button1').click(function(e) { 
    // prevent click action 
    e.preventDefault(); 
    // your code here 
    return false; 
}); 
+0

同樣的事情將適用於輸入按鍵的文本框? – Vicky

+0

它應該無論發生什麼事件都會阻止默認操作。 – doublesharp

15

如果你的「按鈕」是一種button元素,確保你明確地設置type屬性,否則的WebForm將把它作爲默認提交。

<button id="button1" type="button">Go</button> 

如果它是一個input元素,用jQuery這樣做有以下幾點:

$('#button1').click(function(e){ 
    e.preventDefault(); 
    // Code goes here 
}); 

瞭解更多:event.preventDefault()

+0

非常感謝你解釋答案。我是一名新秀,你的第一個陳述澄清了我的想法。謝謝 – Solace

相關問題