【问题标题】:Why does this 'onClick disable=true; this.form.submit();' work on one button but not another?为什么这个 'onClick disable=true; this.form.submit();'在一个按钮上工作而不是另一个?
【发布时间】:2016-11-12 14:33:27
【问题描述】:

此代码“有效”- 按钮被禁用并且表单提交。快乐的日子。

    <form action="timestable_update.php">
    <input type="submit" value="Bank Your Score" name="submitbtn" onclick="this.disabled=true; this.form.submit();">
</form>

此代码不起作用(我正在查看 id="btn" 按钮)...所发生的只是按钮禁用,但没有发生提交...

<form method= "post" action="timestable_submit.php">

        <input type="hidden" name=b value=<?php echo $f;?>>
        <input type="hidden" name=c value=<?php echo $s;?>>
        <input type="submit" name="submit" id="btn" value="<?php echo $d;?>" onclick="this.disabled=true; this.form.submit();">
        <input type="submit" name="submit" id="btn2" value="<?php echo $w1e;?>">

        <input type="submit" name="submit" id="btn3"  value="<?php echo $w2e; ?>">
        </form>

关于为什么的任何想法?

这是完整的代码...

<html>
    <head>
        <!--------------timer---------------->
       <script type="text/javascript">
        var interval;
        var minutes = 0;
        var seconds = 05;
        window.onload = function () {
            countdown('secondsleft');
        }

        function countdown(element) {
            interval = setInterval(function () {
                var el = document.getElementById(element);
                if (seconds == 0) {
                     el.innerHTML = "Time is Up!"; 
                    window.location.assign("timestable_timeout.php")
                    if (minutes == 0) {
                        clearInterval(interval);
                        return;
                    } else {
                        minutes--;
                        seconds = 60;
                    }
                }
                el.innerHTML = seconds;
                seconds--;
            }, 1000);
        }
    </script>

        <!------------------ timer end ------------>
        <style>
            input[type='submit']{
                position: absolute;
                width: 300;
                height: 50;
                color: white;
                background: red;
                 font-size:100%;
            }
        </style>


                </head>
    <body>

        Level: 1<br>Staff, your score so far is: 0      
        <br>Click on the answer to:     2x11    

        <form method= "post" action="timestable_submit.php">

        <input type="hidden" name="b" value="2">
        <input type="hidden" name="c" value="11">
        <input type="submit" name="submit" id="btn" value="22" onclick="this.disabled=true; this.form.submit();">
        <input type="submit" name="submit" id="btn2" value="100">

        <input type="submit" name="submit" id="btn3"  value="30">
        </form>
        <!------ timer----->
        <h1><div id="secondsleft"></div></h1>


    <script>

var btn = document.getElementById("btn");
btn.style.top = 553+ "px";
btn.style.left = 408 + "px";

var btn2 = document.getElementById("btn2");
btn2.style.top = 452+ "px";
btn2.style.left = 290 + "px";

var btn3 = document.getElementById("btn3");
btn3.style.top = 503+ "px";
btn3.style.left = 542 + "px";
        </script>
</body></html>

【问题讨论】:

  • 你检查过生成的html代码吗?请检查页面并这样做。您还可以检查控制台错误...
  • 首先用引号包裹value=&lt;?php echo $f;?&gt;,并添加htmlentities,以防值可能包含引号value="&lt;?php echo htmlentities($f);?&gt;"
  • 正如@FirstOne 所说,您需要发布生成的 html 代码...,否则无法真正帮助您
  • @FirstOne - 已添加代码
  • 我复制粘贴了您生成的完整代码,在 firefox 上对我有用。在 chrome 上它不起作用(控制台显示 Uncaught TypeError: this.form.submit is not a function

标签: javascript php html forms


【解决方案1】:

问题在于第二个按钮上的name="submit" 属性。这会导致 Chrome 出现问题,因为它会覆盖表单的 submit() 函数,从而导致 FirstOne 在他的评论中观察到的错误:

Uncaught TypeError: this.form.submit is not a function

如果您选择不同的名称(就像您为第一个按钮所做的那样),那么它应该可以正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多