我卡住了,試圖使用提交功能(formElement.submit())提交我的表單。 嗯,實際上它發送表單輸入值到後端,但我試圖阻止它並在兩者之間添加ajax。觸發onsubmit無提交按鈕
玉/哈巴狗
form#score-form(method="POST" action="/leaderboard")
input#submit-score(type="hidden" value="" name="submitscore")
//value is fetched from JS
JS
scoreForm.submit();
scoreForm.addEventListener('submit', function(){
//Nothing here will be invoked
});
但是,使用一個提交按鈕時,它按預期工作
form#score-form(method="POST" action="/leaderboard")
input#submit-score(type="hidden" value="" name="submitscore")
button(type="submit")
scoreForm.submit();
scoreForm.addEventListener('submit', function(){
console.log("works"); //logs works
});
一切工作正常,除了它似乎事實.submit()不會調用onsubmit。是否有任何解決方法(使用vanilla js可預測),爲程序提供表單提交的提示,即使使用.submit()?
感謝
您確定在調用事件之前註冊事件偵聽器嗎? –
啊,其實我不是。這和.click()模擬完美結合,謝謝:) – Kazordomo