简体   繁体   English

使用Javascript和HTML创建一个清除按钮

[英]Creating a clear button using Javascript and HTML

Firstly, I know this question has been asked before on here - I have looked at those answers but for some reason it is not working for me at all! 首先,我知道这个问题之前已经被问到了 - 我已经查看了这些答案,但由于某些原因它根本不适合我!

I have a variety of text fields for which I would like to have a 'clear' button to empty the value of the field when clicked. 我有各种各样的文本字段,我希望有一个“清除”按钮,以便在单击时清空字段的值。

This is my JavaScript: 这是我的JavaScript:

function clear() {
    document.getElementById("customerName").value="";
}

and my HTML for this is... 我的HTML就是......

<table border="1" id="orderForm">
    <tr>
        <th colspan="2">Customer Details</th>
    </tr>
    <tr>
        <td id="font">Customer Name</td>
        <td><input type="text" id="customerName"></td>
    </tr>
</table>

<button type="button" id="button1" onClick="clear()">Clear</button>

I have no idea why it won't work, and I've been trying to get it work for ages. 我不知道为什么它不起作用,我一直试图让它工作多年。

clear() is generally not a good function name to define. clear()通常不是一个好的函数名来定义。 It conflicts with document.clear. 它与document.clear冲突。

Also remember you can always just use <input type="reset" value="clear"/> which might be even simpler! 还记得你总是可以使用<input type="reset" value="clear"/>这可能更简单! :) :)

 function clearIt() { document.getElementById('customerName').value = ""; } 
 <table border="1" id="orderForm"> <tr> <th colspan="2">Customer Details</th> </tr> <tr> <td id="font">Customer Name</td> <td><input type="text" id="customerName"></td> </tr> </table> <button type="button" id="button1" onClick="clearIt()">Clear</button> 

正如@Pratyush已经提到的,将函数名称更改为其他内容 - 您的代码将正常工作。

Just to be clear, clear() is perfectly valid Vanilla Javascript. 为了清楚起见, clear() 完全有效的Vanilla Javascript。

It just so happens that document defines a clear() also: 事实上, document定义了一个clear()

在此输入图像描述

...and since your HTML attribute assigned click handler gets executed with a modified scope chain, the document object's clear() comes before your global function in the scope chain (from Javascript: The Definitive Guide): ...并且由于您的HTML属性分配了单击处理程序 ,因此使用修改后的作用域链执行, document对象的clear()位于作用域链中的全局函数之前(来自Javascript:The Definitive Guide):

Event handlers registered as HTML attributes are a special case, however. 但是,注册为HTML属性的事件处理程序是一种特殊情况。 They are converted into top-level functions that have access to global variables but not to any local variables. 它们被转换为顶级函数,可以访问全局变量,但不能访问任何局部变量。 But, for historical reasons, they run with a modified scope chain. 但是,由于历史原因,它们使用修改后的范围链运行。 Event handlers defined by HTML attributes can use the properties of the target object, the containing object (if there is one), and the Document object as if they are local variables. 由HTML属性定义的事件处理程序可以使用目标对象的属性,包含对象(如果有),以及Document对象,就好像它们是局部变量一样。

and then he discusses your exact case: 然后他讨论了你的确切案例:

the modified scope chain of HTML event handlers is a source of pitfalls, since the properties of each of the objects in the chain shadow any properties of the same name in the global object. HTML事件处理程序的修改范围链是陷阱的来源,因为链中每个对象的属性都会影响全局对象中同名的任何属性。 The Document object defines a (rarely used) open() method, for example, so an HTML event handler that wants to invoke the open() method of the Window object must explicitly write window.open instead of open 例如,Document对象定义了一个(很少使用的)open()方法,因此想要调用Window对象的open()方法的HTML事件处理程序必须显式写入window.open而不是open

so, your function could be reached via window.clear() in the HTML: 所以,你的功能可以通过HTML中的window.clear()来实现:

 function clear() { document.getElementById("customerName").value=""; } 
 <table border="1" id="orderForm"> <tr> <th colspan="2">Customer Details</th> </tr> <tr> <td id="font">Customer Name</td> <td><input type="text" id="customerName"></td> </tr> </table> <button type="button" id="button1" onClick="window.clear()">Clear</button> 

First call jquery library and then do the following code instead of yours one 首先调用jquery库然后执行以下代码而不是你的代码

$document.ready(function(){
$("button1").click(function(){
document.getElementById("#customerName").value="";
});
});

I prefer to put all javascript before the end of body tag. 我更喜欢在body标记结束之前放置所有javascript。

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

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