简体   繁体   English

如果没有结果或有 null 值,如何清除输入的值?

[英]How to clear the value of an input if there is no results or if it has a null value?

I am trying to clear the value of an input depending if it finds or not an id, if it finds an existing id, js updates the value of an input, but if it doesn't it keeps the last one found but I need to have the value clear, can someone tell me what is wrong:我正在尝试清除输入的值,具体取决于它是否找到 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 = "";
    }
}

In the method driverdata you don't define variable id_number so it's undefined when you try get element by id在方法 driverdata 中,您没有定义变量id_number所以当您尝试按 id 获取元素时它是未定义的

So if id_number is equal to the parameter of the method you can directly use it所以如果id_number等于方法的参数就可以直接使用

moreover to clear value you are right it's elem.value = ""此外,为了明确价值,你是对的,它是 elem.value = ""

withour yout html i can propose you the following one => your script run没有你的 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM