[英]How to clear the value of an input if there is no results or if it has a null value?
我正在尝试清除输入的值,具体取决于它是否找到 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 = ""
没有你的 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.