简体   繁体   English

提交按钮可在Chrome中使用,但不能在Firefox或IE中使用

[英]Submit button working in Chrome but not Firefox or IE

I have a submit button which seems to be clickable in Chrome but not in FF or IE. 我有一个提交按钮,在Chrome中似乎可以单击,但在FF或IE中却无法单击。 Any help would be appreciated! 任何帮助,将不胜感激! I feel like the formatting of the button is incorrect. 我觉得按钮的格式不正确。

  <button> <img src="http://button_image.png" onclick="mySubmit();" ></button>

The JQ attached to the mySubmit is: 附加到mySubmit的JQ是:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

<script>
function mySubmit(){
    var myForms = $("form");
    myForms.each(function(index) {
        var form = myForms.eq(index);
        var serializedForm = form.serialize();
        serializedForm += '&s_referer_fname='+$('#s_refererFname').val();
        $.post("http://post.aspx", serializedForm, function (data, status) {
            if (status === "success"){
                window.location.href= "http:url.aspx?";
            }  
        });   
    });
}
</script>

In FF and IE it seems the onclick event is captured by the button and not the image tag. 在FF和IE中,似乎onclick事件是通过按钮而不是图像标签捕获的。

If you want to trigger the event cross-browser assign it to the button. 如果要触发事件,则跨浏览器将其分配给按钮。 That way it gets propagated up from the image in browsers that raise it from the image while other browsers capture it on the button right away. 这样,它就可以在浏览器中从图像中传播出来,并从图像中抬起,而其他浏览器则立即在按钮上捕获它。

<button onclick="mySubmit();"> <img src="http://button_image.png" ></button>

Will work in FF, IE and Chrome ► https://jsfiddle.net/aspqwc3p/1/ 将在FF,IE和Chrome中运行►https ://jsfiddle.net/aspqwc3p/1/

If you want an image to be a submit button you you should use the input tag with the type="image" attribute: 如果您希望图像成为提交按钮,则应将input标签与type="image"属性一起使用:

 <input type="image" src="http://button_image.png" onclick="mySubmit();" />

Note that inside your mySubmit function you should disable the default behavior using preventDefault() : 请注意,在mySubmit函数内部,应使用preventDefault()禁用默认行为:

function mySubmit() {
    this.preventDefault();

It's generally recommended not to put any JavaScript in your HTML. 通常建议不要在HTML中放入任何JavaScript。 Try giving your button an ID and using 尝试给按钮一个ID并使用

$('#yourid').on('click', function(e) {
  e.preventDefault();
  .... your code
});

in your javascript. 在您的JavaScript中。 (and put your script tag at the very end of your html) (并将您的脚本标签放在html的最后)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM