2017-05-03 68 views
-1

下面是我的代码,它在每次单击添加按钮并删除该行中的文本框时,以模态弹出形式动态创建文本框当我点击删除按钮,这是工作正常,直到这里的问题是我有JavaScript函数验证月份日期和年份在文本框中,如果我们给任何数字大于12它显示消息,该月应该少于12同样的日期也会接受,直到31日,但如果它大于31它显示错误信息和同样的一年也,但这是为我们的asp文本框完成我怎么能使这个JavaScript函数工作在模式弹出窗口的文本框创建动态如何激活在asp.net中动态创建的文本框中的javascript函数

 <script type="text/javascript"> 
     function GetDynamicTextBox(value) { 
      if (value == "") { 
       FillDropdown() 
       return '<input name = "DynamicTextBox" value = "' + value + '" placeholder="MM/DD/YYYY"></input>&nbsp;&nbsp;<select name = "DynamicTextBox" >"' + Hours + '"</Select><b>:</b><select name = "DynamicTextBox">"' + Min + '"</Select>' + 
          ' <input id="btnAdd123" type="button" value="Add" onclick="AddTextBox()" /><input type="button" value="Remove" onclick = "RemoveTextBox(this)" />' 
       //Min = ""; 
       // Hours = ""; 
       // 
      } 

     } 
     var HHEdit = ""; 
     var MMEdit = ""; 
     function GetDynamicTextBox1(value) { 


      values = value.split(' '); 
      one = values[0]; 
      two = values[1]; 

      values = two.split(':'); 
      three = values[0]; 
      Four = values[1]; 




      HHEdit = three; 
      MMEdit = Four; 
      FillDropdown() 

      return '<input name = "DynamicTextBox" value = "' + one + '" placeholder="MM/DD/YYYY"></input>&nbsp;&nbsp;<select name = "DynamicTextBox" >"' + Hours + '"</Select><b>:</b><select name = "DynamicTextBox">"' + Min + '"</Select>' + 
         ' <input id="btnAdd123" type="button" value="Add" onclick="AddTextBox()" /><input type="button" value="Remove" onclick = "RemoveTextBox(this)" />' 
      // $('.DynamicTextBox').val(one); 
     } 
     function AddTextBox() { 
      var div = document.createElement('DIV'); 
      div.innerHTML = GetDynamicTextBox(""); 
      document.getElementById("TextBoxContainer").appendChild(div); 
     } 
     function AddTextBox1() { 
      var inputCount = document.getElementById('TextBoxContainer').getElementsByTagName('input').length; 
      if (inputCount == "0") { 
       var div = document.createElement('DIV'); 
       div.innerHTML = GetDynamicTextBox(""); 
       document.getElementById("TextBoxContainer").appendChild(div); 
      } 


     } 
     function RemoveTextBox(div) { 
      document.getElementById("TextBoxContainer").removeChild(div.parentNode); 
     } 

     function RecreateDynamicTextboxes() { 
      var values = eval('<%=Values%>'); 
     if (values != null) { 
      var html = ""; 
      for (var i = 0; i < values.length; i++) { 
       html += "<div>" + GetDynamicTextBox1(values[i]) + "</div>"; 
      } 
      document.getElementById("TextBoxContainer").innerHTML = html; 
     } 
    } 

    var Hours = ""; 
    var Min = ""; 
    function FillDropdown() { 
     for (var i = 0; i < 24; i++) { 
      if (i >= 0 && i <= 9) { 
       if (HHEdit != "" && HHEdit == i) { 
        Hours += '<option value="' + i + '" selected="selected">' + " 0" + i + " " + '</option>' 
       } 
       else { 
        Hours += '<option value="' + i + '">' + " 0" + i + " " + '</option>'; 
       } 
      } 
      else { 
       if (HHEdit != "" && HHEdit == i) { 
        Hours += '<option value="' + i + '" selected="selected">' + " " + i + " " + '</option>'; 
       } 
       else { 
        Hours += '<option value="' + i + '">' + " " + i + " " + '</option>'; 
       } 
      } 


     } 
     for (var i = 0; i < 60; i++) { 
      if (i >= 0 && i <= 9) { 
       if (MMEdit != "" && MMEdit == i) { 
        Min += '<option value="' + i + '" selected="selected">' + " 0" + i + " " + '</option>'; 
       } 
       else { 
        Min += '<option value="' + i + '">' + " 0" + i + " " + '</option>'; 
       } 
      } 
      else { 
       if (MMEdit != "" && MMEdit == i) { 
        Min += '<option value="' + i + '" selected="selected">' + " " + i + " " + '</option>'; 
       } 
       else { 

        Min += '<option value="' + i + '">' + " " + i + " " + '</option>'; 
       } 
      } 

     } 
     //$('#Item').append(option); 
    } 


    window.onload = RecreateDynamicTextboxes; 
</script> 

使用月日验证码的javascript

var fdate = document.getElementById('<%=txtFromDate.ClientID%>').value; 
       var tdate = document.getElementById('<%=txtToDate.ClientID%>').value; 
       var fromdate = fdate.split('/'); 
       var fmonth = fromdate[0]; 
       var fdate = fromdate[1]; 
       var fyear = fromdate[2]; 
       if (fmonth > 12) { 
        message += "From Month Should Be Less Than 12." + "\n"; 
       } 
       if (fdate > 31) { 
        message += "From Date Cannot Be Greater Than 31." + "\n"; 
       } 
       if (fyear < 2000 || fyear > 2030) { 
        message += "From Year Should Be In Between 2000 to 2030." + "\n"; 
       } 
       var todate = tdate.split('/'); 
       var tmonth = todate[0]; 
       var tdate = todate[1]; 
       var tyear = todate[2]; 
       if (tmonth > 12) { 
        message += "To Month Should Be Less Than 12." + "\n"; 
       } 
       if (tdate > 31) { 
        message += "To Date Cannot Be Greater Than 31." + "\n"; 
       } 
       if (tyear < 2000 || tyear > 2030) { 
        message += "To Year Should Be In Between 2000 to 2030."+"\n"; 
       } 
       if (message != "") { 
        alert(message); 
        return false; 
       } 
From Date: <asp:TextBox ID="txtFromDate" Width="113px" runat="server" placeholder="mm/dd/yyyy" onkeypress="return IsValidData(event);" ondrop="return false;" 
     onpaste="return false;" onkeyup="this.value=this.value.replace(/^(\d\d)(\d)$/g,'$1/$2').replace(/^(\d\d\/\d\d)(\d+)$/g,'$1/$2').replace(/[^\d\/]/g,'')"></asp:TextBox> <span id="error" style="color: Red; display: none">* Invalid Character</span>&nbsp;&nbsp; 
    To Date: <asp:TextBox ID="txtToDate" Width="113px" runat="server" placeholder="mm/dd/yyyy" onkeypress="return IsValidData(event);" ondrop="return false;" 
     onpaste="return false;" onkeyup="this.value=this.value.replace(/^(\d\d)(\d)$/g,'$1/$2').replace(/^(\d\d\/\d\d)(\d+)$/g,'$1/$2').replace(/[^\d\/]/g,'')"></asp:TextBox><span id="Span1" style="color: Red; display: none">* Invalid Character</span>&nbsp;&nbsp; 
+0

你试着用'onchange'? – Webruster

+0

是的,它使用的是ASP文本框,但它不适用于动态创建的文本框 –

+0

我没有看到将客户端事件处理程序添加到动态创建的客户端控件的部分。除非在创建后向控件显式添加事件处理程序,否则不会发生客户端事件。 –

回答

0

我不知道你实际上已经定义了IsValidData功能。我假设你已经做了什么地方...

你可以通过您IsValidData功能参数与在你的函数触发事件的元素来工作..

而且你应该尝试从内联函数转移到事件监听器。这篇文章(JavaScript click event listener on class)讨论了如何将一个事件监听器应用到一个DOM元素类中。

如果您使用的是jQuery,因为您包含此标记,那么我建议您查看jQuery .on()函数以添加事件处理程序。它会让一切更容易(http://api.jquery.com/on/

如果你不知道一个JavaScript事件监听器是什么,然后从这里开始(https://www.w3schools.com/js/js_htmldom_eventlistener.asp

+0

我不是在我的代码中有无效的数据功能你可以通过编辑我的代码来显示我,因为我是这个概念的新手 –

+0

你需要阅读这两篇文章:https://www.w3schools.com/js/js_function_definition.asp https://www.w3schools.com/js/js_htmldom_eventlistener.asp – l0ul0u

+0

我已经看到这两篇文章的代码是在这两个文章类似于ny javascript函数我的问题是如何激发我的javascript函数在这个return'