2
我得到了form
;
<div id="mainDiv">
<form>
<input name="input1" />
<input name="input2" />
<input name="input3" />
<input name="input4" />
<button id="showForm">Show Form</button>
<button id="sendForm">Submit Form</button>
</form>
</did>
這是我的jQuery
插件。
(function(o){
o.fn.validateInputs = function(options){
var settings = $.extend({
submitButton:'',
submitFunction : function(){
return ""
},options);
$(settings.submitButton).on('click',function(){
//......run the FunctionOfChoiceHere!
//...I tried.
console.log(settings.submitFunction) // This logs an Empty Function
})
}
})(jQuery);
和普通jQuery
:
$('#showForm').on('click',function(){
$('#mainDiv').fadeIn()
var form = $(this).closest('form');
var formData =$(form).serialize();
$('input',form).validateInputs({
submitFunction :myFunctionOfChoice(formData),
submitButton:'#submitForm'
})
})
現在myFunctionOfChoice
。
function myFunctionOfChoice(data){
console.log(data);
}
的問題是,當點擊showForm
按鈕,這將自動運行myFunctionOfChoice
和logs
數據....這是正是我不想。我要求的是,只有當我點擊submitForm
按鈕時,console
應該log
。我怎樣才能做到這一點?
任何幫助表示讚賞!
你會如何傳遞參數? –
你能解釋一下「通過表單的數據」是什麼意思嗎?......這將如何完成?... thanx –
@UniversalGrasp我相應地更新了答案。這種方法也比你最初嘗試實現的要好,因爲表單數據可能會在表單顯示和提交按鈕被按下之間發生變化。 – plalx