Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.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 使用JQuery删除基于条件的元素_Javascript_Jquery - Fatal编程技术网

Javascript 使用JQuery删除基于条件的元素

Javascript 使用JQuery删除基于条件的元素,javascript,jquery,Javascript,Jquery,我试图检查输入文件是否为空,如果为空,并且存在一个名为subm的按钮,我将其删除,但是如果输入文件有一个文件,我将创建该底部。问题是,如果有一个submbotton,因为我选择了file,在我更改input后,让它为空,JQuery不会删除它 如果输入文件为空,如何删除由JQuery创建的按钮 谢谢你的预付款 $('this')应该是$(this),$('this')匹配具有id=this$('this')应该是$(this),$('this')匹配具有id=this的元素,除非您实际拥有具有“

我试图检查
输入文件是否为空,如果为空,并且存在一个名为
subm
的按钮,我将其删除,但是如果
输入文件有一个文件,我将创建该底部。问题是,如果有一个
subm
botton,因为我选择了file,在我更改
input
后,让它为空,
JQuery
不会删除它

如果
输入文件
为空,如何删除由
JQuery
创建的按钮


谢谢你的预付款

$('this')
应该是
$(this)
$('this')
匹配具有
id=this
$('this')
应该是
$(this)
$('this')
匹配具有
id=this
的元素,除非您实际拥有具有“this”id的元素,您可能打算使用
这个
关键字:

$('#img').change(function(){

    if($('#this').val() === '') { 

        $("#subm").remove();

    }  else {

         $('<button id="subm" type="submit">Press</button>').insertAfter('#advice');

   }

});
$('#img')。更改(函数(){
//这将是触发更改事件的#img
if($(this.val()=''){
$(“#subm”).remove();
}否则{
$('Press')。在('advice')之后插入;
}
});

除非您实际有一个id为“this”的元素,否则您可能打算使用
this
关键字:

$('#img').change(function(){

    if($('#this').val() === '') { 

        $("#subm").remove();

    }  else {

         $('<button id="subm" type="submit">Press</button>').insertAfter('#advice');

   }

});
$('#img')。更改(函数(){
//这将是触发更改事件的#img
if($(this.val()=''){
$(“#subm”).remove();
}否则{
$('Press')。在('advice')之后插入;
}
});
使用以下方法:

$('#img').change(function(){
    // this will be the #img that fired the change event
    if($(this).val() === '') {
        $("#subm").remove();
    }  else {
         $('<button id="subm" type="submit">Press</button>').insertAfter('#advice');
    }
});
使用以下命令:

$('#img').change(function(){
    // this will be the #img that fired the change event
    if($(this).val() === '') {
        $("#subm").remove();
    }  else {
         $('<button id="subm" type="submit">Press</button>').insertAfter('#advice');
    }
});

需要注意的一点是,
this
的值也可以根据与javascript和jQuery一起使用的上下文而变化。您现在应该没事了,但是在更复杂的情况下,您可能会遇到问题

这种情况下的最佳实践是立即创建另一个变量,通常称为
self

<input id="img" type="file" name="img" accept="image/*">
<button id="subm" type="submit">Press</button>

如果您使用下一级的另一个函数,则会出现此问题,
现在将引用下一级函数中的
。每当您进入一个新函数时,为
self
定义一个新变量,以便始终锁定此
是什么。(别忘了让这些变量名是唯一的。)

需要注意的一点是,
this
的值也可以根据javascript和jQuery使用的上下文而改变。您现在应该没事了,但是在更复杂的情况下,您可能会遇到问题

这种情况下的最佳实践是立即创建另一个变量,通常称为
self

<input id="img" type="file" name="img" accept="image/*">
<button id="subm" type="submit">Press</button>

如果您使用下一级的另一个函数,则会出现此问题,
现在将引用下一级函数中的
。每当您进入一个新函数时,为
self
定义一个新变量,以便始终锁定此
是什么。(别忘了让这些变量名是唯一的。)

您应该始终有一个按钮。如果输入为空,您可以禁用甚至隐藏它,但没有理由删除它。您应该始终拥有一个按钮。如果输入为空,您可以禁用甚至隐藏它,但没有理由删除它。