首页 > 解决方案 > jQuery更改不适用于动态元素

问题描述

我有一个输入,如果长度小于 1,我想显示一个图标 X。如果长度大于 1,我想打印一个刻度图标:

为此,我想使用函数 change、keyup 和 focusout。

这个功能不起作用,因为我不知道如何正确识别输入。我不能使用输入的 ID,因为该 ID 是动态的。

我试过使用它:

$( "filaa-enq" ).change(function()

$( "filaa-enq" ).keyup(function()

$( "filaa-enq" ).focusout(function()

在此处输入图像描述

<div class="input">
  <label class="text-mida">
     <div class="fila-enq"><input class="filaa-enq" type="input" name="question_1_1_0" value="" maxlength="20" id="question_1_1_0"><span style="display:none" class="validate_ok_enquesta fa fa-check"></span><span style="display:none" class="validate_ko_enquesta fa fa-times"></span></div>
  </label>
</div>
$( ".filaa-enq" ).keyup(function() {
        console.log("ttttt");
        checkEnquesta(this); 
 });

$( "filaa-enq" ).focusout(function() {
        console.log("test");
        checkEnquesta(this); 
  });

$( "filaa-enq" ).change(function() {
        console.log("changees");
        checkEnquesta(this); 
});

function checkEnquesta(element){
    console.log(element);
    if (element.value=="") {
        $('#'+element.id).siblings(".validate_ko_enquesta").hide();
        $('#'+element.id).siblings(".validate_ok_enquesta").hide();
        return false;
    }
    if (element.value.length > 1 ){
        $('#'+element.id).siblings(".validate_ok_enquesta").show();
        $('#'+element.id).siblings(".validate_ko_enquesta").hide();
    }
    else{
        $('#'+element.id).siblings(".validate_ko_enquesta").show();
        $('#'+element.id).siblings(".validate_ok_enquesta").hide();
    }
}
.validate_ok_enquesta{
    float: right;
    margin-right: 6px;
    margin-top: -35px;
    position: relative;
    z-index: 2;
    color:green;
}

.validate_ko_enquesta{
    float: right;
    margin-right: 6px;
    margin-top: -35px;
    position: relative;
    z-index: 2;
    color:red;
}

我怎样才能传递身份证?如果那个id是动态的?

标签: javascripthtmljqueryjsp

解决方案


推荐阅读