【问题标题】:javascript onclick event handlers not functioning properlyjavascript onclick 事件处理程序无法正常运行
【发布时间】:2010-10-12 03:07:48
【问题描述】:

由于某种原因,我的 onclick javascript 事件处理程序无法正常运行。

这是我的标记、脚本和样式:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Quadratic Root Finder</title>

<script>
document.ready = function() {
    document.getElementById('calculate').onClick.calculateQuad()
    {
        var inputa = document.getElementById('variablea').value;
        var inputb = document.getElementById('variableb').value;
        var inputc = document.getElementById('variablec').value;

        root = Math.pow(inputb,2) - 4 * inputa * inputc;
        root1 = (-inputb + Math.sqrt(root))/2*inputa
        root2 = (-inputb + Math.sqrt(root))/2*inputa 

        document.getElementById('root1').value = root1;
        document.getElementById('root2').value = root2;
        if(root<'0')
        {
            alert('This equation has no real solution.')
        }
        else {
            if(root=='0')
            {
                document.getElementById('root1').value = root1
                document.getElementById('root2').value = 'No Second Answer'
            }
            else {
                document.getElementById('root1').value = root1
                document.getElementById('root2').value = root1
                }
            }
    };
    document.getElementById('erase').onClick.document.getElementById('form1').reset();
}
</script>

<style>
#container
{
    text-align: center;
}
</style>

</head>

<body>
<div id="container">
<h1>Quadratic Root Finder!</h1>
<form id="form1">
    a:<input id="variablea" value="" type="text">
    <br/>
    b:<input id="variableb" value="" type="text">
    <br />
    c:<input id="variablec" value="" type="text">
    <br />
    <input id="calculate" value="Calculate!" type="button">
    <input id="erase" value="Clear" type="button">
    <br />
    <br />
    Roots:
    <br />
    <input id="root1" type="text" readonly>
    <br />
    <input id="root2" type="text" readonly>
</form>
</div>
</body>
</html>

有什么可以解释的原因吗?

【问题讨论】:

    标签: javascript javascript-events event-handling onclick


    【解决方案1】:

    这是错误的
    document.getElementById('calculate').onClick.calculateQuad()
    一定是
    document.getElementById('calculate').onClick = function ()

    这也是错误的
    document.getElementById('erase').onClick.document.getElementById('form1').reset();
    修复:
    document.getElementById('erase').onClick = function(){document.getElementById('form1').reset();}

    【讨论】:

    • 我想你的意思是.onclick= calculateQuad(设置函数本身)。我不认为你所拥有的会起作用。不是 100% 确定,但我确信我刚刚提出的内容会起作用。不过,第 2 部分似乎是正确的。
    【解决方案2】:

    实际上并没有像这样的“就绪”事件。如果您想在通常认为的“就绪”点做事,则需要使用框架。否则,您可以使用“onload”而不是“ready”。

    例如,如果您使用 jQuery,您会这样做:

    $(function() {
        var inputa = document.getElementById('variablea').value;
        var inputb = document.getElementById('variableb').value;
        var inputc = document.getElementById('variablec').value;
        // etc ...
    });
    

    没有它,你会这样做:

    window.onload = function() {
      // all your stuff
    };
    

    还有:

     ... .onclick.calculateQuad() {
    

    毫无意义。

    【讨论】:

    • 好的,我刚刚尝试过,因为其他方法不起作用,但我知道了原因,谢谢。
    【解决方案3】:
    .onClick.document.getElementById('form1').reset()
    

    我想你想把它变成:

    .onclick = function () { document.getElementById('form1').reset(); }
    

    【讨论】:

      【解决方案4】:

      如果你可以使用 Jquery

      $(document).ready(function() { $("#variablea").click(函数(元素) { ///....手术 }); });

      您可以使用$(" input").click(function(element){}) 操作所有输入元素

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-27
        • 2013-03-25
        • 2020-04-10
        • 1970-01-01
        • 2019-06-06
        • 1970-01-01
        • 2011-06-20
        • 2022-08-26
        相关资源
        最近更新 更多