How can I apply a dynamic mask in an dynamic form?

前端 未结 2 412
孤独总比滥情好
孤独总比滥情好 2021-01-16 22:06

I have a problem with my dynamic form. This input is:



        
相关标签:
2条回答
  • 2021-01-16 22:34

    Pamio is on the right track. But appending the script to the page doesn't seem to work(?). The following works for me:

    $('#plusPhone').click(function(){
       var HTML = 'this contains an input field with the class you want to mask';
       jQuery('#element-you-want-to-append-to').append(HTML);
       jQuery('.class-you-want-masked').mask('99-99-9999');
       //Just call the mask AFTER appending the input, and it should be applied.
    )};
    
    0 讨论(0)
  • 2021-01-16 22:36

    You will have to add it dynamically just like this. This is an untested code but the idea remains the same.

    $('#plusPhone').click(function(){
      $('#appendPhone').append("<div class=\"row\"><div class=\"col-md-6\"><div class=\"form-group\"><label class=\"control-label col-md-3\">Phone</label><div class=\"col-md-9\"><input type=\"text\" class=\"form-control\" placeholder=\"EX: (XX)-XXXX-XXXX\" name=\"phone[]\" id=\"phone\"></div></div></div></div>"); 
    
      var code = "<script>$('#telefone').mask('(00)-0000-00000');</scr"+"ipt>";
      $('#appendPhone').append($(code)[0]);
    )};
    

    Here is an example of how to execute js by appending it dynamically in htmls - enter link description here This way you can append the code snippet to dynamically created htmls. Hope it helps

    0 讨论(0)
提交回复
热议问题