简体   繁体   English

Jquery 屏蔽输入\不适用于动态添加的输入

[英]Jquery Masked input \not working on dynamically added inputs

I am creating a signup form for a project however having an issue.我正在为一个项目创建注册表单,但遇到了问题。 What my form does is enable a user to click a "add contact" button and dynamically have more input boxes appear on the page so that the user can type in additional users.我的表单所做的是使用户能够单击“添加联系人”按钮并动态地在页面上显示更多输入框,以便用户可以输入其他用户。 All is working except for the jquery masked.除了被屏蔽的 jquery 之外,一切正常。 The jquery works an any default text input on the page but does not work on any javascript generated input. jquery 适用于页面上的任何默认文本输入,但不适用于任何 javascript 生成的输入。

Hoping someone understands what I am explaining I am using the below for the masked input http://digitalbush.com/projects/masked-input-plugin/希望有人理解我在解释什么我正在使用下面的屏蔽输入http://digitalbush.com/projects/masked-input-plugin/

And below is my javascript and html下面是我的 javascript 和 html

   <script language="javascript">
     fields = 0;
     function addInput() {
       if (fields != 5) {
        document.getElementById('text').innerHTML += "<div class='row-fluid'><div class='span5 clearfix'><h4 class='heading_b'>Company Address</h4><input type='text' name='company_address[]' class='span11'></div></div><div class='row-fluid'><div class='span5 clearfix' style='width:300px'><h4 class='heading_b'>Company City</h4><input type='text' name='company_city[]' class='span5'></div><div class='span4 clearfix' style='width:300px'><h4 class='heading_b'>Company State</h4><select name='company_state[]' class='span4'><option value='AL'>AL</option><option value='AK'>AK</option><option value='AZ'>AZ</option><option value='AR'>AR</option><option value='CA'>CA</option><option value='CO'>CO</option><option value='CT'>CT</option><option value='DE'>DE</option><option value='DC'>DC</option><option value='FL'>FL</option><option value='GA'>GA</option><option value='HI'>HI</option><option value='ID'>ID</option><option value='IL'>IL</option><option value='IN'>IN</option><option value='IA'>IA</option><option value='KS'>KS</option><option value='KY'>KY</option><option value='LA'>LA</option><option value='ME'>ME</option><option value='MD'>MD</option><option value='MA'>MA</option><option value='MI'>MI</option><option value='MN'>MN</option><option value='MS'>MS</option><option value='MO'>MO</option><option value='MT'>MT</option><option value='NE'>NE</option><option value='NV'>NV</option><option value='NH'>NH</option><option value='NJ'>NJ</option><option value='NM'>NM</option><option value='NY'>NY</option><option value='NC'>NC</option><option value='ND'>ND</option><option value='OH'>OH</option><option value='OK'>OK</option><option value='OR'>OR</option><option value='PA'>PA</option><option value='RI'>RI</option><option value='SC'>SC</option><option value='SD'>SD</option><option value='TN'>TN</option><option value='TX'>TX</option><option value='UT'>UT</option><option value='VT'>VT</option><option value='VA'>VA</option><option value='WA'>WA</option><option value='WV'>WV</option><option value='WI'>WI</option><option value='WY'>WY</option></select></div><div class='span3 clearfix'><h4 class='heading_b'>Company Zip</h4><input type='text' name='company_zip[]' class='span3' maxlength='5'></div></div><div class='row-fluid'><div class='span5 clearfix' style='width:300px'><h4 class='heading_b'>Landload/Mortgage Company</h4><input type='text' name='company_landlordcompany[]' class='span8'></div><div class='span5 clearfix' style='width:250px'><h4 class='heading_b'>Rent/Mortgage Amt</h4><input type='text' name='companyrentamt[]' class='span4'></div><div class='span5 clearfix' style='width:300px'><h4 class='heading_b'>Landlord Contact Name</h4><input type='text' name='business_landlordname[]' class='span8'></div><div class='span5 clearfix' style='width:290px'><h4 class='heading_b'>Landlord Contact Phone</h4><input type='text' name='business_landlordphone[]' class='span6 mask_phone'></div></div>";
        fields += 1;
        } else {
        document.getElementById('text').innerHTML += "<br />Only 5 addresses are allowed.";
        document.form.add.disabled=true;
        }
        }

The above is in the head of my html page then in the body in my form I have以上是我的 html 页面的头部然后在我的表格中的正文中我有

<div id='text'>  </div>

If I take any of the inputs that are listed in the above javascript that has a masked input and add it to the body of the page the masked input works.如果我采用上面 javascript 中列出的任何具有屏蔽输入的输入并将其添加到页面正文,则屏蔽输入有效。 I am hoping someone may have a suggestion.我希望有人可以提出建议。

I have tried taking the js file that contains the masked input code and putting it at the bottom of the page and also tried having it at the top of the page but have had no luck.我尝试将包含屏蔽输入代码的 js 文件放在页面底部,也尝试将它放在页面顶部,但没有成功。 Thanks谢谢

Somewhere in your script you must have initialized the masked plugin for the inputs similar to: $("#phone").mask("(999) 999-9999"); 您必须在脚本中的某个位置为输入类似于以下内容初始化被屏蔽的插件: $("#phone").mask("(999) 999-9999"); When you add elements dynamically in addInput() they are not initialized. 在addInput()中动态添加元素时,不会对其进行初始化。 You could do that. 你可以那样做。 However I think it would be easier for you to add everything in the body from the start, and have the "dymamic" elements hidden. 但是,我认为从一开始就将身体中的所有内容都添加进去,并隐藏“动态”元素会更容易。 That way you will have them initialized. 这样,您将对其进行初始化。 When the user clicks "add contact" you can show them one after another like in this fiddle . 当用户单击“添加联系人”时,您可以像在此小提琴中那样一个接一个地显示他们。

edit: updated link 编辑:更新的链接

You must call your mask after adding your new inputs.添加新输入后,您必须调用掩码。

//clone or add your new input
var template = $(".template").clone();
template.appendTo(".newdivs");

//Then call your masks
$('.mask-tel').mask('(000) 000-0000');

If you have multiple masks, put all your masks in a function. You can call that function on pageload and everytime you add a new input.如果您有多个掩码,请将所有掩码都放在 function 中。您可以在页面加载时和每次添加新输入时调用 function。

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

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