[英]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.