清除图像文本框中的内容

时间:2013-07-30 07:52:23

标签: javascript

任何人都可以帮助清除文本框中的内容。我已尝试使用javascript

清除文本框中的内容
<input name="newKey" id="newKey" type="text" value="helo" size="38" maxlength="45"/>
<span class="btnClr" id="clear" onclick=clearThis("newKey"></span>


function clearThis(target){
    target.value= "";
}

4 个答案:

答案 0 :(得分:4)

您正在清除名为'target'的字符串:)

您想要清楚的是DOM元素本身:

function clearThis(target) {
    target = document.getElementById(target);
    target.value = "";
}

此外,您的onclick属性需要引用不明确:

onclick='clearThis("search")'

Here is a working fiddle


另一方面,请考虑使用不那么突兀的JavaScript。它更容易维护和发展。调试属性字符串中的代码可能是一个真正的噩梦,并可能会产生可移植性问题。

类似的东西:

var clear = document.getElementById("clear");
var search = document.getElementById("search");

function clearThis(element) {
    element.value = "";
}

clear.onclick = function(){
    clearThis(search);    
}

HTML中没有JavaScript

Here is a fiddle of that

答案 1 :(得分:1)

按身份使用获取元素..... http://jsfiddle.net/Q7fRB/230/

function clearThis(target){


        document.getElementById(target).value= "";
    }

答案 2 :(得分:0)

<强> working Fiddle

$("#clear").click(function(){
    $("#newKey").val('');
});

答案 3 :(得分:0)

你可以简单地做到这一点

function clearThis(target){
    document.getElementById(target).value = "";

    }