validate dynamically added textbox

核能气质少年 提交于 2019-12-24 03:16:09

问题


In the below code how to validate for null values for only the dynamically added text box i.e,r_score1,r_score2.........

  <script>
   function validate()
   {
      //How to validate the r_Score.. textfields
   }
  $(document).ready(function() {
   for(var i=0;i< sen.length;i++)
   {
     row += '<input type="text" name="r_score'+i+'" id="r_score'+r_count+'" size="8" />';
    }
   $('#details').append(row);
   });
   </script>
   <div id="details"><input type="text" name="score' id="score" size="8" /></div>
  <input type="button" value="Save" id="print" onclick="javascript:validate();" />

回答1:


Give the text boxes a class name and use class selector

function validate()
   {
      var boxes = $("#details input:text.classname");
      boxes.each(function(){
          var currentVal = $(this).val();
          // do your validation here
      });
   }



回答2:


$("#details input[name^='r_score']").each(function(nr){

    if($(this).val() === "") alert("Field "+(nr+1)+" is empty");

}); 



回答3:


Use live event handler for dynamically added content.



来源:https://stackoverflow.com/questions/2682030/validate-dynamically-added-textbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!