简体   繁体   English

尝试从按钮清除文本栏时出错

[英]Error when trying to clear a text bar from a button

When I click the button ( id="ClearText4" ) to clear a text bar, an error occurs and the page refreshes.当我单击按钮 ( id="ClearText4" ) 以清除文id="ClearText4"时,会发生错误并刷新页面。 What am I doing wrong?我究竟做错了什么?

<form method="post" target="imgChart4">
    <input type="submit" value="Chart 4" />
    <input type="text" id="ChartBar4" name="ChartBar4" style="width: 286px;"><br>
</form>

<img src="https://sitedeapostas-com.imgix.net/assets/local/Company/logos/betfair_logo_transp.png?auto=compress%2Cformat&fit=clip&q=75&w=263&s=c1691b4034fd0c4526d27ffe8b1e839c" name="Chart 4"/>

<form  action="" method="post" id="ClearText4">
    <button onclick="limpartexto4()">Limpar Texto 4</button>
</form>

<script type="text/javascript">
    function limpartexto4() {
        var btn = document.getElementById('ClearText4');
        btn.onclick = function(){ document.getElementById('ChartBar4').value="" };
    }
</script>

You have things turned around a little You can remove the form tag around that button and actually get rid of the javascript function as well.您的情况有所好转 您可以删除该按钮周围的表单标记,实际上也可以删除 javascript 功能。 Since there's only one thing to do, just put that in the onclick attribute.由于只有一件事要做,只需将其放在 onclick 属性中即可。

<form method="post" target="imgChart4">
  <input type="submit" value="Chart 4" />
  <input type="text" id="ChartBar4" name="ChartBar4" style="width: 286px;"><br>
</form>
<img src="https://sitedeapostas-com.imgix.net/assets/local/Company/logos/betfair_logo_transp.png?auto=compress%2Cformat&fit=clip&q=75&w=263&s=c1691b4034fd0c4526d27ffe8b1e839c" name="Chart 4" />
<button onclick="document.getElementById('ChartBar4').value=''">Limpar Texto 4</button>

 <form method="post" target="imgChart4"> <input type="submit" value="Chart 4" /> <input type="text" id="ChartBar4" name="ChartBar4" style="width: 286px;"><br> </form> <img src="https://sitedeapostas-com.imgix.net/assets/local/Company/logos/betfair_logo_transp.png?auto=compress%2Cformat&fit=clip&q=75&w=263&s=c1691b4034fd0c4526d27ffe8b1e839c" name="Chart 4"/> <form action="" method="post" id="ClearText4"> <button onclick="limpartexto4()">Limpar Texto 4</button> </form> <script type="text/javascript"> function limpartexto4() { var btn = document.getElementById('ClearText4'); btn.onclick = function(e){ e.preventDefault(); document.getElementById('ChartBar4').value="" }; } </script>

Your form has a method = "post" that's why this happens.您的表单有一个method = "post"这就是发生这种情况的原因。

<form method="post" target="imgChart4">

Because Form is posted to action="" properties mentioned.因为 Form 被发布到 action="" 提到的属性。

You can use e.preventDefault() in the function to avoid default action.您可以在函数中使用e.preventDefault()来避免默认操作。

function limpartexto4() {
    var btn = document.getElementById('ClearText4');
    btn.onclick = function(e){ 
     e.preventDefault();
  document.getElementById('ChartBar4').value="" };
}

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

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