在追加一行并调用其中的时差函数时生成动态id

dynamic id generating when appending a row and calling time difference function in it?

本文关键字:函数 时差 id 动态 追加 调用 一行      更新时间:2023-09-26

Am使用trent的timepicker插件以表格格式生成timepicker。开始时间和结束时间在同一行,根据这一行计算时差。为了生成多个时间选择器,我使用了class而不是id。问题是,我想生成动态id,并在其中附加一行calculate()。

以下是我当前的代码:

$(document).ready(function() {
    //table grid structure follows here
    $("#AddRow").click(function() {
        var row = '<tr><td>' +'<input type=text /></td>' + '<td><input type=text class="timepicker" value=""/></td>' + '<td><input type=text class="timepicker" value=""/></td>' + '<td><input type=text id= "difference" /></td>' + '<td><button>delete</button>' + '</td></tr>';
        $("#table").append(row);
    });
    $('body').on('focus', ".timepicker", function() {
        $(this).timepicker({});
    });
    $("#table").on("click", "button", function() {
        $(this).closest("tr").remove();
    });
    //table structure ends here
});

有什么方法可以实现吗??

IE浏览器不支持"sessionStorage"
试试这个。。。

$(document).ready(function(){
var i=0;
$('input.timepicker').each(function(){
    i++;
    $(this).attr("id","timepicker_"+i);
});

});

生成动态Id的一个想法是在会话存储中存储一个数值,该值将连接到常量前缀。该数值在使用后将递增。类似于:

var id = sessionStorage.getItem("id");
if (!id) {
    id = 0;
}
var idValue = "myId" + id.toString();
sessionStorage.setItem("id", id++);