Javascript 如何解决以下掩码输入问题?

Javascript 如何解决以下掩码输入问题?,javascript,jquery,jquery-plugins,Javascript,Jquery,Jquery Plugins,我有一个脚本来屏蔽一个文本框,在这里它 <script src="http://jquery-joshbush.googlecode.com/ files/jquery.maskedinput-1.2.2.min.js" type="text/javascript"></script> <script type="text/javascript"> jQuery(function($) { $('#j').mask('99:99');

我有一个脚本来屏蔽一个文本框,在这里它

<script src="http://jquery-joshbush.googlecode.com/
files/jquery.maskedinput-1.2.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(function($) {

      $('#j').mask('99:99');
         });
</script>

jQuery(函数($){
$('#j')。掩码('99:99');
});
我还有一个脚本,可以在单击按钮时动态添加文本框

<script type="text/javascript">
    function addRow(tableID) {

                var table = document.getElementById(tableID);

                var rowCount = table.rows.length;
                var row = table.insertRow(rowCount);

                var colCount = table.rows[0].cells.length;

                for(var i=0; i<colCount; i++) {

                    var newcell = row.insertCell(i);

                    newcell.innerHTML = table.rows[0].cells[i].innerHTML;
                    //alert(newcell.childNodes);
                    switch(newcell.childNodes[0].type) {
                        case "text":
                                newcell.childNodes[0].value = "";
                                newcell.childNodes[0].id="j";
                                alert(newcell.childNodes[0].id);
                                break;
                        case "checkbox":
                                newcell.childNodes[0].checked = false;
                                break;
                        case "select-one":
                                newcell.childNodes[0].selectedIndex = 0;
                                break;
                    }
                }
            }

            function deleteRow(tableID) {
                try {
                var table = document.getElementById(tableID);
                var rowCount = table.rows.length;

                for(var i=0; i<rowCount; i++) {
                    var row = table.rows[i];
                    var chkbox = row.cells[0].childNodes[0];
                    if(null != chkbox &amp;&amp; true == chkbox.checked) {
                        if(rowCount <= 1) {
                            alert("Cannot delete all the rows.");
                            break;
                        }
                        table.deleteRow(i);
                        rowCount--;
                        i--;
                    }


                }
                }catch(e) {
                    alert(e);
                }
            }
    </script>

函数addRow(tableID){
var table=document.getElementById(tableID);
var rowCount=table.rows.length;
var row=table.insertRow(rowCount);
var colCount=table.rows[0].cells.length;

对于(var i=0;i,掩码插件没有将live绑定到DOM中的新元素,而是从$('#j')选择器绑定到当时存在的元素

  • 使用类而不是ID(因为页面上不能合法地有两个ID相同的元素),并且
  • 如有必要,在添加到DOM后,对动态创建的元素重新调用
    .mask()
  • 使用。 然后给出要屏蔽该类的所有元素
    maskme
    。现在可以执行以下操作:

    $(".maskme").livequery(function(){
        $(this).mask('99:99');
    });
    

    即使在代码首次运行后,这也会屏蔽添加的输入。

    首先不要在输入上使用ID

    <input type="text" name="STime[]" class="jClass"/>
    
    
    
    第二,如果您使用jQuery,那么就使用它。这更容易阅读

    <script type="text/javascript">
        function addRow(tableID) {
          var table = $("#" + tableID); //get the table
          var firstRowClone = $("tr:first", table).clone(); //clone the first row
          $("input:checkbox",firstRowClone).attr("checked", false);  // set all checkboxes to unchecked
          $("select", firstRowClone).each(function () { //Set all select lists to select first item
            this.selectedIndex = 0;
          }
          table.append(firstRowClone); //append the cloned row to the table.
          $("input:text", firstRowClone).val("").mask("99:99"); //set all input type="text" with value of "" and sets the mask on the clone.
    
        });
    
        function deleteRow(tableID) {
          $("#" + tableId + " tr:not(:eq(0))").remove(); //Remove all rows except the first row.         
        }
    
    
        $(document).ready(function {
          $('.jClass').mask('99:99'); //sets the mask on any rows loaded initially
        });
    
    </script>
    
    
    函数addRow(tableID){
    var table=$(“#”+tableID);//获取表
    var firstRowClone=$(“tr:first”,table).clone();//克隆第一行
    $(“输入:复选框”,firstRowClone).attr(“选中”,false);//将所有复选框设置为未选中
    $(“选择”,firstRowClone).each(函数(){//设置所有选择列表以选择第一项
    此参数。selectedIndex=0;
    }
    table.append(firstRowClone);//将克隆的行追加到表中。
    $((“输入:text”,firstRowClone).val(“”).mask(“99:99”);//将所有输入type=“text”的值设置为“”,并在克隆上设置掩码。
    });
    函数deleteRow(tableID){
    $(“#”+tableId+“tr:not(:eq(0))”)。remove();//删除除第一行以外的所有行。
    }
    $(文档).ready(函数){
    $('.jClass').mask('99:99');//在最初加载的任何行上设置掩码
    });
    
    创建一个事件绑定到目标类的输入(如果不需要,请不要使用ID)。使用jQuery.on方法

    例如:

    <input class="classSelector" />
    
    <script>  
      $(document).on("focus", "classSelector", function() { 
        $(this).mask("99:99");
      });
    </script>
    
    
    $(document).on(“焦点”,“类选择器”,function(){
    $(此).mask(“99:99”);
    });
    

    您可以动态创建任意数量的输入,并使用on事件绑定屏蔽它们。您创建的具有该类的每个新输入都会将该事件处理程序附加到它。

    不要在脚本中使用Id use class来屏蔽php中的多用途时间掩码
    ghj

    值得注意的是,HTML元素的id属性应该是唯一的。您不应该有两个id相同的输入元素。请改用Class,并指定一个通用的类名。我不明白??你能给我一个示例吗??我不明白??你能给我一个示例吗??我在用.live()挠头函数,认为没有合适的事件。感谢您的发布,我学到了一些东西:)嘿,它在第一个文本框上工作,但在动态生成的文本框上不工作。@Alex-应该可以工作。动态添加的文本元素是否也有'class=“maskme”?是的,我这样做了……但不工作……newcell.childNodes[0].class=“j”name=“ETime[]”class=“j”@Alex-尝试运行jQuery.livequery.registerPlugin(“addRow”)在我发布代码之前。如果这不起作用,我唯一的另一个建议是改用jQuery来添加新的输入元素,而不是纯javascript。没错,假设addRow是唯一需要屏蔽的添加新文本元素的地方,这很可能是。非常好的建议。@alex…我编辑了上面的代码。…d当你把克隆行附加到DOM之后,它会应用这个掩码。但是如果你说不工作的话,它会很有帮助。声明“它不起作用”根本没有帮助。请给出更多的细节。工作得像个符咒!工作了!谢谢。请考虑给你的答案加上一些解释和细节。
    <script type="text/javascript">
        function addRow(tableID) {
          var table = $("#" + tableID); //get the table
          var firstRowClone = $("tr:first", table).clone(); //clone the first row
          $("input:checkbox",firstRowClone).attr("checked", false);  // set all checkboxes to unchecked
          $("select", firstRowClone).each(function () { //Set all select lists to select first item
            this.selectedIndex = 0;
          }
          table.append(firstRowClone); //append the cloned row to the table.
          $("input:text", firstRowClone).val("").mask("99:99"); //set all input type="text" with value of "" and sets the mask on the clone.
    
        });
    
        function deleteRow(tableID) {
          $("#" + tableId + " tr:not(:eq(0))").remove(); //Remove all rows except the first row.         
        }
    
    
        $(document).ready(function {
          $('.jClass').mask('99:99'); //sets the mask on any rows loaded initially
        });
    
    </script>
    
    <input class="classSelector" />
    
    <script>  
      $(document).on("focus", "classSelector", function() { 
        $(this).mask("99:99");
      });
    </script>