如果没有结果或输入值为空,如何清除输入值



我正在尝试清除输入的值,这取决于它是否找到id,如果找到现有id,js会更新输入的值。但如果没有,它会保留最后一个找到的值,但我需要清除值,有人能告诉我出了什么问题吗:

function driverdata(valueid)
{
var numero_id = valueid;
//console.log(valueid)
var idselect = document.getElementById('driver'+id_number).value;
document.getElementById("idinsearch"+ id_number).value = idselect;
//console.log(idselect);
var placa = document.getElementById("searchable"+idselect).value;
console.log(placa);
if (placa != null) {
document.getElementById("placa"+ id_number).value = placa;
} else {
document.getElementById("placa"+ id_number).value = "";
}
}

在方法driverdata中,您没有定义变量id_number,因此当您尝试按id 获取元素时,它是未定义的

因此,如果id_number等于该方法的参数,则可以直接使用

此外,为了清除值,你是对的,它是elem.value="">

使用我们的yout html,我可以向您推荐以下内容=>你的脚本运行

function driverdata(numberId)
{
var idselect = document.getElementById('driver'+numberId).value;
document.getElementById("idinsearch"+ numberId).value = idselect;
var placa = document.getElementById("searchable"+idselect).value;
if (placa != null) {
document.getElementById("placa"+ numberId).value = placa;
} else {
document.getElementById("placa"+ numberId).value = "";
}
}
<div onclick="driverdata(1)">
click me<br/>
driver<input id="driver1" value="1"/><br/>
idinsearch<input id="idinsearch1"/><br/>
<div id="searchable1">
input that will be clear <input id="placa1" value="test"/>
</div>
</div>

最新更新