admin管理员组文章数量:1389857
I have form:
<form id="form1" class="f_class">
<input type="button" class="jq-button" value="jq-button"/>
</form>
And js code:
var f=document.getElementById("form1");
f.addEventListener("submit", function(){alert("addEventListener_submit");},false);
$(".jq-button").click(function(){$("#form1").submit();});
(Jsbin)
When I click to jq-button alert does not show. Why?
UPDATE
I have external js code, which use addEventListener
, and I can't change it. But in my code I want use jQuery submit. Can I bined them?
I have form:
<form id="form1" class="f_class">
<input type="button" class="jq-button" value="jq-button"/>
</form>
And js code:
var f=document.getElementById("form1");
f.addEventListener("submit", function(){alert("addEventListener_submit");},false);
$(".jq-button").click(function(){$("#form1").submit();});
(Jsbin)
When I click to jq-button alert does not show. Why?
UPDATE
I have external js code, which use addEventListener
, and I can't change it. But in my code I want use jQuery submit. Can I bined them?
2 Answers
Reset to default 4just correct it to:
<input type="submit" class="jq-button" value="jq-button"/>
$().submit() triggers the jquery submit event and not the eventlisterner. I would have written it with jQuery all of the binding of events like:
var f = $("#form1");
f.on("submit", function() { // on requires jquery 1.7+
alert("submit");
});
$(".jq-button").on("click", (function(e) {
$(this).closest("form").trigger("submit");
e.preventDefault();
});
Edit:
you could do something like:
var f=document.getElementById("form1");
f.addEventListener("submit", function(){
alert("addEventListener_submit");
},false);
$(".jq-button").click(function(){
var c = $(this),
cc = c.clone().attr("type","submit");
c.replaceWith(cc);
cc.click();
});
if you can´t change the HTML markup.
本文标签: javascriptaddEventListener submit and jQuery submitStack Overflow
版权声明:本文标题:javascript - addEventListener submit and jQuery submit - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744690704a2619977.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论