简体   繁体   English

onclick方法在Chrome中不起作用

[英]onclick method doesn't work in Chrome

This code works in Mozilla Firefox, but not in Chrome. 该代码在Mozilla Firefox中有效,但在Chrome中不起作用。

<html>
<head>
<script language="Javascript">
<!--
function OnButton1()
{
    document.Form1.action = "response1.php"
    document.Form1.target = "_blank";    // Open in a new window

    document.Form1.submit();             // Submit the page

    return true;
}

function OnButton2()
{
    document.Form1.action = "response2.php"
    document.Form1.target = "_blank";    // Open in a new window

    document.Form1.submit();             // Submit the page

    return true;
}
-->
</script>
<noscript>You need Javascript enabled for this to work</noscript>
</head>
<body>
<form name="Form1" method="post">
Your Name <input type="text" name="name" size="10" /><br />
<INPUT type="button" value="Button1" name=name onclick="OnButton2();OnButton1();">
</form>
</body>
</html>

What can we use in the place of onclick to trigger the two actions? 我们可以用什么代替onclick来触发这两个动作?

Thanks @dfsq for providing the answer ! 感谢@dfsq提供答案!

It's actually possible in Chrome too if you add little timeout in the second submission 如果您在第二次提交中添加很少的超时,实际上在Chrome中也有可能

<html>

<head>
    <script language="Javascript">
        <!--
        function OnButton1() {
            document.Form1.action = "response1.php"
            document.Form1.target = "_blank";
            document.Form1.submit();
        }

        function OnButton2() {
            document.Form1.action = "response2.php"
            document.Form1.target = "_blank";
            document.Form1.submit();
        }
         -->
    </script>
</head>

<body>
    <noscript>You need Javascript enabled for this to work</noscript>
    <form name="Form1" method="post">
        Your Name
        <input type="text" name="name" size="10" />
        <br />
        <input type="button" value="Button1" name="name" onclick="OnButton1();setTimeout(OnButton2, 50)">
    </form>
</body>

</html>

HTML button : HTML按钮:

<input type="button" value="Button1" name="name" onclick="return OnButton1();">

JavaScript : JavaScript:

function OnButton1()
{
    document.Form1.action = "response1.php"
    document.Form1.target = "_blank";    // Open in a new window

    document.Form1.submit();             // Submit the page

    OnButton2();                        //Calling second function here
}

function OnButton2()
{
    document.Form1.action = "response2.php"
    document.Form1.target = "_blank";    // Open in a new window

    document.Form1.submit();             // Submit the page

    return true;
}

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

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