Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 允许空值,同时在提交后保留输入值_Javascript_Jquery_Html - Fatal编程技术网

Javascript 允许空值,同时在提交后保留输入值

Javascript 允许空值,同时在提交后保留输入值,javascript,jquery,html,Javascript,Jquery,Html,我在表单中有几个可选的输入字段。我希望在提交后保留它们的输入值 我正在阅读这篇文章,但由于输入字段都是可选的,我得到一个错误,无法在web控制台中设置null的属性值 提交 可以使用条件三元数将null替换为空字符串: document.getElementById('l2').value = getl2()? getl2() : ''; document.getElementById('l3').value = getl3()? getl3() : ''; 或者可以使用相同的逻辑返回空

我在表单中有几个可选的输入字段。我希望在提交后保留它们的输入值

我正在阅读这篇文章,但由于输入字段都是可选的,我得到一个错误,
无法在web控制台中设置null的属性值


提交

可以使用条件三元数将null替换为空字符串:

document.getElementById('l2').value = getl2()? getl2() : ''; 
document.getElementById('l3').value = getl3()? getl3() : '';
或者可以使用相同的逻辑返回空字符串,而不是null:

function getl3(){
    if (localStorage.getItem("user_selected_l3") !== null) {
        return localStorage.getItem("user_selected_l3")?
               localStorage.getItem("user_selected_l3") : '';
    }   
}

如果getl2()和getl3()函数发现空字符串,则可以返回空字符串。谢谢。我发现在函数中处理空值就可以做到这一点。
function getl3(){
    if (localStorage.getItem("user_selected_l3") !== null) {
        return localStorage.getItem("user_selected_l3")?
               localStorage.getItem("user_selected_l3") : '';
    }   
}