简体   繁体   English

如果某些文本框是动态生成的,如何进行客户端验证

[英]How to do client side validations if some text boxes are generated dynamically

I'm working on a form which has this add button, if the button is clicked a text box is foing to generate and i have a minus button to cancel it. 我正在处理具有此添加按钮的表单,如果单击该按钮,则可能生成一个文本框,并且我有一个减号按钮来取消它。

my problem is i have to work on client side validations in this page 我的问题是我必须在此页面上进行客户端验证

the code i used to generate new text boxes is 我用来生成新文本框的代码是

i = 0;
var counter = 2;

$("#addButton").click(function () {
    optionCount++;
i++;


var newTextBoxDiv = $(document.createElement('div'))
     .attr("id", 'TextBoxDiv'+i);
   newTextBoxDiv.after().html('<label>Option : </label>' +
      '<input type="text" class="add_input" name="textbox'  + counter + '"   id="textbox' + counter + '" > <input type="button" class="rem_img minusclick" id="removeButton'+i+'"  alt="Remove"  title="Remove" onclick="RemoveButton(\''+i+'\',\''+counter+'\')">');

 newTextBoxDiv.appendTo("#TextBoxesGroup");
    optionIds.push(counter);
    counter++;

The validation is to be done after clicking the submit button. 单击提交按钮后,将完成验证。 and i need to display an error message beside each empty text box(that is generated by clicking add button) 我需要在每个空文本框旁边显示一条错误消息(通过单击添加按钮生成)

im confused where to start and what exactly i have to do for this Please help.. 即时通讯感到困惑,从哪里开始,我到底要为此做什么,请帮助..

I add onchange method textbox; 我添加了onchange方法文本框;

i = 0;
var counter = 2;

$("#addButton").click(function () {
    optionCount++;
i++;


var newTextBoxDiv = $(document.createElement('div'))
     .attr("id", 'TextBoxDiv'+i);
   newTextBoxDiv.after().html('<label>Option : </label>' +
      '<input type="text" onchange="Validate(this,\"ValueControl\")" class="add_input" name="textbox'  + counter + '"   id="textbox' + counter + '" > <input type="button" class="rem_img minusclick" id="removeButton'+i+'"  alt="Remove"  title="Remove" onclick="RemoveButton(\''+i+'\',\''+counter+'\')">');

 newTextBoxDiv.appendTo("#TextBoxesGroup");
    optionIds.push(counter);
    counter++;

Create This function; 创建此功能;

 function Validate(elementObject,validType){
            if(validType=="ValueControl"){
                if($(elementObject).val()==''){
                    $(elementObject).css("background-color","#000000");
                }
            }
        }

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

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