简体   繁体   English

第二个输入 onclick() function 不起作用

[英]Second inputs onclick() function doesn't work

There were similar questions, but not exactly what I'm looking for.有类似的问题,但不完全是我想要的。 I have a filter for my table with 1 button that changes the value of a textbox to the currant date.我的表格有一个过滤器,带有 1 个按钮,可将文本框的值更改为当前日期。 That works fine, but right next to it, theres a button to clear the content in the textbox.这很好用,但在它旁边,有一个按钮可以清除文本框中的内容。 This second button doesnt seem to work.这第二个按钮似乎不起作用。

My code:我的代码:

 function today() { var today = new Date(); var dd = String(today.getDate()).padStart(2, '0'); var mm = String(today.getMonth() + 1).padStart(2, '0'); var yyyy = today.getFullYear(); today = yyyy + '.' + mm + '.' + dd; document.getElementById("myInput").value = today; } function clear() { document.getElementById("myInput").value = ""; }
 <div class="form-inline"> <label for="myInput">Filter By:</label> <input class="input-xsmall" type="text" maxlength="10" placeholder="yyyy.mm.dd" id="myInput" style="width: 100px; margin-bottom: 10px; display: inline-block"> <input type="button" value="Today" onclick="today()"> <input type="button" value="Clear" onclick="clear()"> </div>

Not sure how to get it to work.不知道如何让它工作。 When I switched the onclick function in the first input as clear() it worked so for some reason the second input button doesnt work.当我将第一个输入中的 onclick function 切换为clear()时,它起作用了,所以由于某种原因,第二个输入按钮不起作用。 How can I fix that?我该如何解决?

Try to change the name of that clear() function.尝试更改clear() function 的名称。 And also consider looking into event handlers .并且还考虑研究事件处理程序

You need to change the js function name from clear() to something else.您需要将 js function 名称从 clear() 更改为其他名称。 Because clear() is a javascript built in function/method.因为 clear() 是一个 javascript 内置函数/方法。 Javascript Clear fields Function Not Working? Javascript 清除字段 Function 不工作?

clear is not a reserved keyword. clear不是保留关键字。 It is a scope problem这是 scope 问题

But still if you want to use same function name.但是,如果您想使用相同的 function 名称。 can try below code.可以试试下面的代码。

Changing your existing function onClick="clear()" to window.clear() can fix your issue.将现有的 function onClick="clear()"更改为window.clear()可以解决您的问题。

Refer this Ans参考这个答案

<input type="button" value="clear" onclick="window.clear()">

Unlike what is written here, clear is not a word reserved in js .与这里写的不同, clear不是js中保留的词。

Here is the list of reserved words( from s3 site )这是保留字列表(来自 s3 站点 这是保留字列表

Felix Kling Explained here , why sometimes it acts as a reserved word Felix Kling在这里解释一下,为什么有时它会充当保留字

Or just add an event listener instead或者只是添加一个事件监听器

document.querySelector("[type='button'][value='Clear']").addEventListener("click", clear);

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

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