[英]submit a form with mooTools, and have the forms inline onsubmit event fire…help
我正在編寫一個類,用一個自定義按鈕替換文檔中所有input type="button"
和input type="submit"
的類,這實際上是一個填充在anchor標簽內的跨度,我已經編寫了所有內容,起作用,除了使用JavaScript提交表單時不觸發內聯onsubmit
事件。
實際上,即使編寫靜態錨標簽
<a href="javascript:void(0)" onclick="document.FORMNAME.submit();">Submit</a>
不會觸發表單的內聯onsubmit
事件,但是<input type="submit" />
會觸發該事件……無論如何,必須有一種方法可以使用javascript觸發此onsubmit事件。
以下是即時通訊使用的代碼段:
click: function() {
//get parent form
var thisForm = this.getParent('form');
//fire onsubmit event
thisForm.fireEvent('submit'); //this doesnt work!
//submit form...the event isn't fired here either!
thisForm.submit();
}
有人有什么想法嗎? 此類不需要任何配置,並且有一些我們希望將其插入使用cms系統的舊站點,並且在表單上有很多內聯提交事件,我們需要考慮這些事件
謝謝
更新,
我接受了建議,並提出了以下建議:
if(this.getParent().onsubmit()) {
this.getParent().submit();
}
可以很好地用於評估表單onsubmit事件並根據其輸出提交表單,但是如果該函數根本不存在怎么辦? 有任何想法嗎?
在表單上觸發submit
不會觸發它的內聯onSubmit
,這是默認的JavaScript行為(找到此代碼: http : //www.codestore.net/store.nsf/unid/DOMM-4QS3RL/ )
您可以嘗試自己致電onsubmit
,這對我onsubmit
:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/mootools/1.3.1/mootools-yui-compressed.js"></script>
<script type="text/javascript" charset="utf-8">
window.addEvent('domready', function(e) {
$$('span.submit').each(function(el) {
el.addEvent('click', function(e) {
this.getParent().onsubmit();
});
});
});
</script>
</head>
<body>
<form method="post" onsubmit="alert('hi');" class="form">
<input type="text" value="test" />
<span class="submit">Submit the form</span>
</form>
</body>
</html>
您可能需要嘗試使用此代碼,因為它會檢查onsubmit事件是否已附加為事件偵聽器,而不是作為硬編碼屬性。 如果將事件作為偵聽器附加,則[form element] .onsubmit()部分將不起作用。
function myOnSubmit(formId){
var f = document.getElementById(formId);
if(document.createEvent){
var e = document.createEvent('Event');
e.initEvent('submit',true,false);
f.dispatchEvent(e);
} elseif(document.createEventObject){
var e = document.createEventObject('Event');
e.button = 1;
f.fireEvent('onsubmit',e);
} elseif(typeof(f.onsubmit) == 'function'){
f.onsubmit();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.