无法在Chrome中将<input type =“email”/>重置为空白(在Firefox和IE中正常工作)

时间:2015-06-24 20:00:23

标签: javascript google-chrome html-email

我的网站上有一个input type="email"的字段。在某些验证中,我将文件重置为空,请按以下方式检查我的代码:

    function clickme(){
        document.getElementById("abc").value="";
    }
    <input type="email" id="abc" placeholder="abc">
    <input type="button" onclick="clickme()" value="go">

如果我插入空格并单击按钮,那么它工作正常,它在Firefox和IE中重置为空白。如果是Chrome,则无法重置。 (注意:类型不能更改为输入类型=“文本”)

FIDDLE

Chrome版本:版本43.0.2357.130 m

3 个答案:

答案 0 :(得分:2)

这很棘手,但你可以这样做:

function clickme(){
    document.getElementById("abc").value="a";
    document.getElementById("abc").value="";
}

它适用于白色空间和其他字符以及chrome 40。

http://jsfiddle.net/z0arsoq7/2/

答案 1 :(得分:1)

我能够通过使用jQuery事件监听器而不是内联的onclick属性来解决这个问题。请参阅fiddle,如下所示。

合理的解释(基于我的测试,而不是文档):当前版本的Chrome要求在使用onclick调用JS函数之前定义它们。奇怪的是,当我第一次加载它时,你original fiddle为我抛出了一个未定义的函数错误。在我写这个答案的时间里,我再次测试了,它运作正常。

HTML:

<input type="email" id="abc">
<input id="specialButton" type="button" value="go">

和JS:

function clickme(){
    document.getElementById("abc").value="";
}

$(document).on("click tap", "#specialButton",function() {
    clickme();
});

答案 2 :(得分:0)

如果将其包装在<form>元素中,则可以使用重置输入类型。

<form>
  <input type="email" id="abc">
  <input type="reset" value="go">
</form>

https://jsfiddle.net/santhucool007/z0arsoq7