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" />
在这里可以正常工作: