2011-01-24 41 views
0

我建立了驗證一個簡單的插件,設置一些選項可編輯, 後和騎自行車的所有字段後,我寫道:建築jQuery插件

$(this).submit(function() { 

其中「這」是主要因素(表格)。

現在我想知道在asp.net中也使用這個插件,所以如果沒有使用html表單,那裏只有一些輸入到div,並點擊一個特定的按鈕,它就會開始...

所以我知道在這裏我不得不改變提交...並試圖將它綁定按鈕的點擊...我不知道如何解決這個問題?

有人能幫忙嗎?

回答

0

您需要幫助來綁定點擊事件?爲此你寫

$('#buttonID').click(function(e) 
{ 
    // do some logic 

    e.preventDefault(); // make sure that ie. a href is not followed 
}); 
+0

我需要做的,但功能必須開始在父DIV驗證!怎麼做? –

+0

我不知道你的確切的dom結構,但如果你只需要父母,你可以使用父()函數http://api.jquery.com/parent/ –

0

你應該看看我在做什麼here。我的插件劫持表單提交數據跨域到我的服務器,而不是託管服務器。整體方法可用於您的驗證。

這是我的代碼的一個快速和骯髒的例子。

$.fn.myplugin= function (options) { 

    var settings = { 
     setting1: undefined, 
     setting2: undefined, 
     setting3: undefined, 

     beforeSubmit: function() { return true; }, 
     afterSubmit: function() { }, 
     approve: function() { }, 
     decline: function() { }, 
     error: function() { }, 
     exception: function (xhr, status, error) { } 
    }; 

    // If options exist, lets merge them 
    // with our default settings 
    if (options) { 
     $.extend(settings, options); 
    } 

    //ensure this is attached only to a FORM element 
    if (!$(this).is("form")) 
     throw ('Specified object is not a valid form. This plugin can only be attached to a form element.'); 

    $(this).live("submit", function (event) { 
     var result = true; 

     //NEVER EVER EVER allow the form to submit to the server 
     event.preventDefault(); 

     if (settings.beforeSubmit) 
      result = settings.beforeSubmit(); 

     if (result != null && !result) 
      return false; 

     //do stuff 

    }); //end live submit 

}; 

然後,使用看起來像這樣

$('#form-id').myplugin({ 
    setting1: 'value', 
    setting2: 'value', 
    setting3: 'value' 
}); 
+0

我不明白我可以刪除表單並允許一個簡單的div循環和驗證... –