为什么javascript只工作一次?
我有javascript代码,它复制输入文件的值,并在文本框中实时传递为什么javascript只工作一次?,javascript,javascript-events,Javascript,Javascript Events,我有javascript代码,它复制输入文件的值,并在文本框中实时传递 <script> function copyit(){ var thephoto=document.getElementById('thephoto').value; var fileonchange=document.getElementById('fileonchange').value; if(!thephoto==fileonchange){ document.getElementById('file
<script>
function copyit(){
var thephoto=document.getElementById('thephoto').value;
var fileonchange=document.getElementById('fileonchange').value;
if(!thephoto==fileonchange){
document.getElementById('fileonchange').value=thephoto;
}
}
window.setInterval("copyit()", 500);
</script>
Choose File : <input type="file" id="thephoto"><br>
Here Is the file name : <input type="text" id="fileonchange">
函数copyit(){
var thephoto=document.getElementById('thephoto').value;
var fileonchange=document.getElementById('fileonchange').value;
如果(!thephoto==fileonchange){
document.getElementById('fileonchange')。值=照片;
}
}
setInterval(“copyit()”,500);
选择文件:
以下是文件名:
遗憾的是,这只工作一次,然后在再次更改文件时停止粘贴值。(我的意思是你应该重新加载页面以再次工作)
如果有缓存或其他东西,是吗?您可以自己尝试代码来查看
谢谢大家语法错误。你需要=代码>表示不等式的运算符:
if (thephoto != fileonchange) {
!照片
实际上反转了它的布尔表示(即true
变成false
,反之亦然,null
变成true
)。=
实际上比较了两个操作数的相等性。语法错误。你需要=代码>表示不等式的运算符:
if (thephoto != fileonchange) {
!照片
实际上反转了它的布尔表示(即true
变成false
,反之亦然,null
变成true
)。=
实际上比较两个操作数的相等性。使用==代码>用于严格不等式
或者,如果您想使用与您编写的语法类似的语法,请执行以下操作:
如果(!(thephoto==fileonchange)){
使用!==
严格不等式
或者,如果您想使用与您编写的语法类似的语法,请执行以下操作:
如果(!(thephoto==fileonchange)){
是完全正确的,但是我每500毫秒使用一个计时器来完成这个简单的任务似乎相当繁重
为什么不直接使用
的onchange
事件,例如:
window.onload = function () {
var thephoto = document.getElementById('thephoto');
var fileonchange = document.getElementById('fileonchange');
thephoto.onchange = function () {
// this function will be executed when the user changes the file
fileonchange.value = this.value;
};
};
检查上面的例子。是完全正确的,但是我每500毫秒使用一个计时器来完成这个简单的任务似乎相当繁重
为什么不直接使用
的onchange
事件,例如:
window.onload = function () {
var thephoto = document.getElementById('thephoto');
var fileonchange = document.getElementById('fileonchange');
thephoto.onchange = function () {
// this function will be executed when the user changes the file
fileonchange.value = this.value;
};
};
检查上面的示例。输入元素,如W3C所说,接受onchange
方法。为什么不这样做:
<input type="file" id="thephoto" onchange="copyit()">
不使用可怕的setInterval?输入元素,正如W3C所说,接受onchange
方法。为什么不这样做:
<input type="file" id="thephoto" onchange="copyit()">
不要使用可怕的设置间隔?尝试将IF行更改为:
if(thephoto!=fileonchange){
尝试将IF行更改为:
if(thephoto!=fileonchange){
CMS,Onchange在ie上不起作用,你应该点击页面中的某个地方启动粘贴值的功能:(CMS,Onchange在ie上不起作用,你应该点击页面中的某个地方启动粘贴值的功能:(亲爱的,这与性别无关:)只要有一本好书/教程,动机和心态。快乐编码。亲爱的,这与性别无关:)只要有一本好书/教程,动机和心态。快乐编码。