简体   繁体   English

验证动态附加的文本框字段-jQuery

[英]Validate dynamically appended text box fields-jQuery

I have a dynamic input field that gets appended after a plus button. 我有一个动态输入字段,该字段在加号按钮后附加。

在此处输入图片说明

第二张img

The corresponding id of these fields are answer0, answer1, answer2 and so on. 这些字段的对应ID为answer0,answer1,answer2等。 That means after button click the id will be dynamically appended to the text field. 这意味着单击按钮后,该ID将动态添加到文本字段中。 Now I want to validate these fields. 现在,我要验证这些字段。 My validation code is as follows 我的验证代码如下

function showValidation(response) {
var respArray = JSON.parse(response.responseText).errors;
for(var i=0;i<=(Object.keys(respArray).length);i++){
 var optionss= 'Enter Answers.';
 if($("#answer"+i).val()==''){
      $('#answer'+i+' + span').html('');
      $('#answer'+i).after('<span class="' + errTextboxClass + '" style="color:#e03b3b">' + optionss+ '</span>'); 
      $('#answer'+i).focus();
    }
}
}

I am checking till response error length. 我正在检查直到响应错误的长度。 But before giving values in these fields, validation works properly(fig 1). 但在这些字段中提供值之前,验证工作正常(图1)。 But if I enter values for first 2 fields as in the image above, the validation message does not shows for the third field (fig 2). 但是,如上图所示,如果我输入前两个字段的值,则不会显示第三个字段的验证消息(图2)。 Because at this stage the id is answer2 and the loop 'i' value checks 0 first and next checks 1. So inside loop answer0 and answer1 are having values so the validation stops there. 因为在此阶段,id为answer2,所以循环“ i”的值先检查0,然后再检查1。因此,在循环内部,answer0和answer1具有值,因此验证在那里停止。 I need to get validation for the next fields too. 我也需要对下一个字段进行验证。 Thanks in advance. 提前致谢。

My HTML and corresponding append function 我的HTML和相应的附加函数

 <input class="form-control" name="answer0[]" id="answer0" placeholder="OPTION 1">
 <a class="add-option" onclick="AppendOption()"><img src="{{asset('admin/images/icn-add-option.png')}}" alt=""></a>


   function AppendOption(){
    var k=1;
    $('#appendOption').append('<div class="form-group row"><div class="col-md-4"><input class="form-control" name="answer0[]" id="answer'+k+'" placeholder="OPTION" ></div></div>');
    k++;
     }

If the fields are required you should mark them as required otherwise you validate every field. 如果必填字段,则应将其标记为必填,否则将验证每个字段。 In your case another way for validating could look like this 在您的情况下,另一种验证方式可能如下所示

function showValidation(response) {
    var respArray = JSON.parse(response.responseText).errors;
    $('.form-group input.form-control').each(function(){
        if ($(this).val() == '') {
            $(this).next('span').html('');
            $(this).after('<span class="' + errTextboxClass + '" style="color:#e03b3b">' + optionss+ '</span>');
            $(this).focus();
        }
    });
}

Since I don't know how and where the showValidation() is called I can't improve it further. 由于我不知道showValidation()的调用方式和调用位置,因此无法对其进行进一步的改进。

In your AppendOption function, you set k=1 This is an invalid option once you reach the third entry (option 2). AppendOption函数中,将k=1设置为有效。一旦到达第三个条目(选项2),这将是无效的选项。 You should instead detect that, better yet still make it context sensitive when it executes. 相反,您应该检测到这一点,更好的是在执行时仍使其上下文敏感。 I did this by adding a answer-item class and detecting how many we have and using that number instead. 为此,我添加了一个answer-item类,并检测了我们有多少,然后改用该数字。

I wrapped all this in a <div id="options-container"> so I would have a place to hook the event handler (delegateTarget) https://api.jquery.com/event.delegateTarget/ 我将所有这些都包装在<div id="options-container">因此我有一个钩住事件处理程序(delegateTarget) https://api.jquery.com/event.delegateTarget/的地方

I would not have used an ID here and instead used classes, but that is not part of the question but more rather the cause of it. 我不会在这里使用ID而是使用类,但这不是问题的一部分,而是原因。

 $('.options-container').on('click','.add-option',function(event){ let k= $(event.delegateTarget).find('.answer-item').length; $(event.delegateTarget).append('<div class="form-group row"><div class="col-md-4"><input class="form-control answer-item" name="answer0[]" id="answer' + k + '" placeholder="OPTION" ></div></div>'); }); function showValidation(response) { var respArray = JSON.parse(response.responseText).errors; for (var i = 0; i <= (Object.keys(respArray).length); i++) { var optionss = 'Enter Answers.'; if ($("#answer" + i).val() == '') { $('#answer' + i + ' + span').html(''); $('#answer' + i).after('<span class="' + errTextboxClass + '" style="color:#e03b3b">' + optionss + '</span>'); $('#answer' + i).focus(); } } } 
 <div id="options-container"> <input class="form-control answer-item" name="answer0[]" id="answer0" placeholder="OPTION 1"> <a class="add-option"><img src="{{asset('admin/images/icn-add-option.png')}}" alt=""></a> </div> 

I tried to display the error messages inside an input array loop and I got the answer. 我试图在输入数组循环中显示错误消息,然后得到了答案。

    var result = document.getElementsByTagName("input");
    var optionss= 'Enter Answers.';
    for (var j = 0; j < result.length; j++) {
     if($("#answer"+j).val()==''){
      $('#answer'+j+' + span').html('');
      $('#answer'+j).after('<span class="' + errTextboxClass + '" style="color:#e03b3b">' + optionss+ '</span>'); 
      $('#answer'+j).focus();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM