在单击的href中使用JS函数清除文本框

时间:2014-04-23 08:59:10

标签: javascript jquery html

我有这个功能:

<script>
    function ClearTextBox(textbox_name) {
        $(textbox_name).val("")
    }
    </script>

从文本输入中删除值

我试图用它来调用它:

<a href="javascript:void();" onclick="ClearTextBox('#customercompany1')">Clear</a>

但不清除文本框

4 个答案:

答案 0 :(得分:0)

您应该使用Unobtrusive JavaScript

试试这段代码。

$('a').on('click',function(){
 $('#customercompany1').val("");
});

答案 1 :(得分:0)

对我来说似乎工作正常..但控制台显示以下错误

Uncaught SyntaxError: Unexpected token )

这是由于javascript:void();属性中的href造成的。

所以我将标记更改为

<a href="#" onclick="return ClearTextBox('#customercompany1');">Clear</a>

<input type="text" id="customercompany1" value="test value" />

和脚本

function ClearTextBox(textboxname) {
        $(textboxname).val(" ");
         return false;
    }

希望这会有所帮助......

<强> DEMO HERE

答案 2 :(得分:0)

像这样编写你的函数:

   function ClearTextBox(myInput) {
        var txtBox = document.getElementById(myInput);
        txtBox.value = "";
        return false;
    }

并写下你的html:

  <a href="javascript:void(0);" onclick="return ClearTextBox('customercompany1');">Clear</a>
        <input type="text" id="customercompany1" />

答案 3 :(得分:-1)

需要确定您的HTML,但我猜您的文本框如下所示:

 <input type="text" name="customercompany1"/>

它需要看起来像这样:

 <input type="text" name="customercompany1" id="customercompany1"/>

'#'jQuery选择器匹配id属性,而不是name属性。

(如果你使用了textarea而不是输入,那就是这样)