简体   繁体   English

Onclick没有做任何事情

[英]Onclick isn't doing anything

So here I am wasting few hours on why isn't this thing working. 因此,我在这里浪费了几个小时,以了解为什么这东西不起作用。 I have a bigger function not working, so I decided to test this small one, yet it isn't working either. 我有一个较大的功能无法正常工作,因此我决定测试一下这个较小的功能,但它也无法正常工作。 There are no errors, but nothing is happening after clicking the button. 没有错误,但是单击该按钮后没有任何反应。

<form>
            <input type="text" id="textField">
            <input id="filtrooo" type="submit" value="Click">
    </form>
<script>
        document.getElementById("filtrooo").onclick = function(){
            document.getElementById("textField").innerHTML ="txt";  
        };
</script>

What is wrong here? 怎么了

That's woking now: 现在令人惊叹:

 <form> <input type="text" id="textField"> <input id="filtrooo" type="button" value="Click"> </form> <script> document.getElementById("filtrooo").onclick = function(){ document.getElementById("textField").value = "txt"; }; </script> 

Instead of innerHTML you should write value . 而不是innerHTML您应该编写value And another problem is that when you click submit button the page refreshes and the value of the input clears. 另一个问题是,当您单击“提交”按钮时,页面将刷新,并且输入值将被清除。 So, instead of type="submit" here I use type="button" 因此,我在这里使用type="button"而不是type="submit" type="button"

Try this: 尝试这个:

var filtrooo = document.getElementById("filtrooo");

function functionName() {
    document.getElementById("textField").value ="txt";
}

filtrooo.addEventListener("click", functionName); 

First, with a submit button, the page will be reloaded so you won't see what your code do. 首先,使用提交按钮,将重新加载页面,因此您将看不到代码的作用。 Then, don't use .innerHTML . 然后,不要使用.innerHTML Use .value instead 使用.value代替

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

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