2010-06-14 66 views
0

試圖使用jquery開發web窗體。調用相同函數的一個窗體上的幾個按鈕

我需要的是在一個窗體上有幾個(不知道有多少個)按鈕。

所有這些按鈕都必須調用一個相同的函數,並將一個參數傳遞給該函數。該函數必須做一些post方法,但我可以處理它。

所以,我的主要問題是,我不知道如何開發JS,將調用特定的jQuery功能。

你能幫我嗎?

+0

有人說JSON? – Anders 2010-06-14 11:52:15

回答

1

或者,你可以給一個普通類的所有環節,做這樣的事情

樣本HTML

<a class="yourclass" param1="value1" href=#">Text</a> 

現在腳本

$(".yourclass").click(function() { 

var param = $(this).attr('param1'); 
//now do the remaing 

}); 
+0

tnx,但在這種情況下,我怎樣才能將參數傳遞給jquery函數? – user198003 2010-06-14 12:04:20

+0

參數........................好的看到更新 – Starx 2010-06-14 12:09:18

1
$(':button').bind('click', myfun); 
3

可以使用jQuery函數(jQuery or $)使用幾乎任何CSS3 selector(和一些special jQuery ones,像:button   —喊出"just somebody"爲)來找到按鈕,然後使用click功能掛鉤一個處理程序,像這樣:

$('input[type=button]').click(function(event) { 
    // Here, `this` is the raw DOM element for the button. 
    // You can use $(this) to get a jQuery wrapper for it. 
}); 

click只是爲bind('click', ...)簡寫。

在「過客」的值到處理程序方面,你可以做到這一點具有事件調用已編碼到它的價值,這樣的功能:

$(':button').click(function(event) { 
    // Here, `this` is the raw DOM element for the button. 
    // You can use $(this) to get a jQuery wrapper for it. 
    doSomethingNifty("foo"); 
    return false; // Do this if you want to prevent the default action 
}); 
function doSomethingNifty(arg) { 
    alert(arg); 
} 

現在對任何按鈕頁面會顯示一條提示「foo」。

最後:如果要防止按鈕的默認操作(如果有),則從處理程序返回false,如上所述。

相關問題