2010-02-02 77 views
1

我有一個頁面(ASP.NET 3.5)與一些按鈕,其中一些是保存按鈕,一些不是。我必須是通用的,即不能通過ID調用單獨的控件。我的保存按鈕可能具有屬性UserSubmitBehavior = true。在點擊事件處理程序中,我需要區分我按下的按鈕類型,因此:jQuery服務器端按鈕

$('input[type=submit]').bind('click', function(e) 
{ 
    //need to know how to set up this condition 
    if (this.type = "submit") 
    { 

      //do something here 
    } 
    else 
    { 
      //do something else 

    } 


}); 

我該怎麼做?

回答

3

如果我理解正確,您有多種類型的按鈕,既有常規按鈕又有提交按鈕。你可以做到這一點在一個函數,像這樣:

$('input[type=submit], input[type=button]').bind('click', function(e) 
{ 
    if ($(this).attr('type') == "submit") 
    { 
      //do something here 
    } 
    else 
    { 
      //do something else 
    } 
}); 

你也可以把它分解成一個更可讀的方式,但:

$('input[type=button]').bind('click', function(e){ 
    //do button stuff here 
}); 

$('input[type=submit]').bind('click', function(e){ 
    //do submit stuff here 
}); 

就個人而言,我更喜歡第二種方法。

+0

感謝您的回覆。是的,我更喜歡第二,所以我會盡快嘗試。我想如果我設置UseSubmitBehavior = false,那麼它呈現爲'按鈕'類型,而不是'提交' – Victor 2010-02-02 16:26:41

0

你可以使用一個CSS類:

<asp:Button CssClass="SubmitButton" ... /> 

和:

$('input.SubmitButton').bind(..); 

將這項工作的嗎?

+0

是的,但後來我無法區分頁面上的文本框非提交按鈕 – Victor 2010-02-02 16:22:14

0

我已經在場景中做了這個小技巧,我需要在GridView控件中標識標籤或文本框。我要做的是添加一個自定義屬性,以幫助我在頁面上識別它們。例如:

<asp:Label ID="lblSpecial" runat="server" Text="Whatever" MyCustomeAttr="SpecialLabel"> 
<asp:Label ID="lblSpecial2" runat="server" Text="Whatever" MyCustomeAttr="SpecialLabel2"> 

這是從那裏相當簡單的使用jQuery來獲取該自定義屬性,通過.attr()函數或通過選擇吧:

$("span[id$='_lblSpecial2'][MyCustomeAttr='SpecialLabel2']"); 
+0

這不會違反XHTML合規性嗎? – Spidey 2010-02-02 17:00:58