2017-07-22 45 views
0

任何人都可以帮我把这两个JavaScript放在一起吗?把两个JavaScript重叠在一起输入

控制台正在向我发送这两个内嵌文件的错误。

<script type="text/javascript"> 
    var 
    first = document.getElementById('jform_username'), 
    second = document.getElementById('jform_email1'), 
    third = document.getElementById('jform_email2'); 

    first.onkeyup = function() { 
     second.value = first.value; 
     third.value = first.value; 
    };  
    </script> 

    <script type="text/javascript"> 
    var 
    firstp = document.getElementById('jform_password1'), 
    secondp = document.getElementById('jform_password2'); 

    firstp.onkeyup = function() { 
     secondp.value = firstp.value; 
    };  
    </script> 

如果您也可以提供,为什么这两个可以创建错误,这将是巨大的解释。

我认为它与功能有关,但我不知道。

下面是实际问题的图片。

enter image description here

这是一个基于@Shubham Singla解决方案

enter image description here

在此先感谢了新的问题。

+0

你能给我们提供错误信息吗? – Roman

+0

第一步是删除“

1

问题在于文档“准备好”之前,您正在尝试.getElementById

使用下面的代码等待文档“准备就绪”,然后才能获取元素。

document.addEventListener("DOMContentLoaded", function(event) { 
    //content here 
}); 

检查下面的代码片段:

<script type="text/javascript"> 
 
document.addEventListener("DOMContentLoaded", function(event) { 
 
    var 
 
    first = document.getElementById('jform_username'), 
 
    second = document.getElementById('jform_email1'), 
 
    third = document.getElementById('jform_email2'); 
 

 
    first.onkeyup = function() { 
 
     second.value = first.value; 
 
     third.value = first.value; 
 
    };  
 
    
 
    var 
 
    firstp = document.getElementById('jform_password1'), 
 
    secondp = document.getElementById('jform_password2'); 
 

 
    firstp.onkeyup = function() { 
 
     secondp.value = firstp.value; 
 
    };  
 
}); 
 
</script> 
 

 
<input type="text" id="jform_username" /> 
 
<input type="text" id="jform_email1" /> 
 
<input type="text" id="jform_email2" /> 
 
<input type="text" id="jform_password1" /> 
 
<input type="text" id="jform_password2" />

+0

我改变了你的建议,但是这个问题正在显示。请参阅我编辑的新问题中的图像。它说“Uncaught TypeError不能设置属性'onkeyup'”谢谢 – lupoll88