Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击a href中的JS函数清除文本框_Javascript_Jquery_Html - Fatal编程技术网

Javascript 单击a href中的JS函数清除文本框

Javascript 单击a href中的JS函数清除文本框,javascript,jquery,html,Javascript,Jquery,Html,我有这个功能: <script> function ClearTextBox(textbox_name) { $(textbox_name).val("") } </script> 函数ClearTextBox(textbox\u名称){ $(文本框名称).val(“”) } 从文本输入中删除值的 我试图用以下方式来称呼它: <a href="javascript:void();" onclick="ClearTextB

我有这个功能:

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

函数ClearTextBox(textbox\u名称){
$(文本框名称).val(“”)
}
从文本输入中删除值的

我试图用以下方式来称呼它:

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

但它并没有清除您应该使用的文本框

试试这个代码

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

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

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

它需要像这样:

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


“#”jQuery选择器匹配的是id属性,而不是name属性


(如果您使用的是文本区域而不是输入,则这是正确的)

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

Uncaught语法错误:意外标记)

这是由于
javascript:void()
href
属性中

所以我把分数改成了

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

<input type="text" id="customercompany1" value="test value" />
希望这有帮助


像这样编写函数:

   function ClearTextBox(myInput) {
        var txtBox = document.getElementById(myInput);
        txtBox.value = "";
        return false;
    }
  <a href="javascript:void(0);" onclick="return ClearTextBox('customercompany1');">Clear</a>
        <input type="text" id="customercompany1" />
然后像这样编写html:

   function ClearTextBox(myInput) {
        var txtBox = document.getElementById(myInput);
        txtBox.value = "";
        return false;
    }
  <a href="javascript:void(0);" onclick="return ClearTextBox('customercompany1');">Clear</a>
        <input type="text" id="customercompany1" />

在这里可以正常工作: