简体   繁体   English

jquery 加载 append 日期选择器 function

[英]jquery load append datepicker function

I have the following function that produces numbered input fields with id s and name s of the current timestamp they're were created.我有以下 function 生成编号输入字段,其中idname是它们创建的当前时间戳。 I'm trying to attach a datepicker to each one created, hence the unique/timestamp id's.我正在尝试为每个创建的日期选择器附加一个日期选择器,因此是唯一/时间戳 ID。

Can anybody suggest a way for me to go about doing this?任何人都可以为我建议 go 关于这样做的方法吗?

I believe the datepicker function needs to be produced under each input field created but I don't know how to produce a JavaScript function with JavaScript. I believe the datepicker function needs to be produced under each input field created but I don't know how to produce a JavaScript function with JavaScript. I'm thinking maybe I can use jQuery's load() function and call a PHP script that produces a unique function and loads it into a div.我在想也许我可以使用 jQuery 的load() function 并调用 PHP 脚本,该脚本会生成一个独特的 function 并将其加载到 div 中Is that the best way to tackle this?这是解决这个问题的最好方法吗? Thanks!谢谢!

<script>
    var number = 0;
    var num;
    $('#add_date').click(function(event) {
    number++;
    var d=new Date();
    var year = d.getFullYear();
    var day = d.getDate();
    var month = d.getMonth() + 1;
    if (month<10){var month = "0"+month;}
    if (day<10){var day = "0"+day;} 
    var fullyear = month+"/"+day+"/"+year;
    num = event.timeStamp
    $('#box').append( number+". <input type='text' id='" + num + "' name='" + num + "' value='"+ fullyear + "' size='10'/><br><br>");
    var idtag = "#"+num;
});
</script>

Why not just set up the datepicker when you create the input?为什么不在创建输入时设置日期选择器?

var picker = $("<input/>", {
  type: 'text',
  id: num,
  name: num,
  value: fullyear
}).datepicker();

$('#box').append(number).append(picker);

Also you should make "id" values that look like valid identifiers instead of plain numbers.此外,您应该使“id”值看起来像有效标识符而不是普通数字。

Look at @Pointy 's answer for an actual solution, he was quicker than me, my answer will not actually solve your problem, I just would like to mention a few points to note.查看@Pointy的实际解决方案答案,他比我快,我的回答实际上并不能解决您的问题,我只想提几点注意事项。

Try to indent your code properly, so it's easy to read for you after looking at it in a month's time.试着正确地缩进你的代码,这样一个月后你就能很容易地阅读它。 You might know now what it does exactly, but it will be a pain to figure it out in the long term.你现在可能知道它到底做了什么,但从长远来看,弄清楚它会很痛苦。

As unlikely as it is, it can't be guaranteed that the same event won't fire twice in the same millisecond, I would avoid using event.timeStamp for generating unique IDs.尽管不太可能,但不能保证同一事件不会在同一毫秒内触发两次,我会避免使用event.timeStamp生成唯一 ID。 It's just a personal preference though, it will probably never happen, I just don't like to rely on timers for uniqueness.不过,这只是个人喜好,可能永远不会发生,我只是不喜欢依靠计时器来获得独特性。 You have your incrementing number variable already, you should use that, that will definitely be unique.你已经有了递增的number变量,你应该使用它,那肯定是唯一的。

When writing HTML into a string, I would rather use the proper standard markup.在将 HTML 写入字符串时,我宁愿使用正确的标准标记。 Use ' as your string boundaries and " for your HTML attributes.使用'作为字符串边界,使用"作为 HTML 属性。

Lastly, inside your if(month<10){...} condition, don't redefine the variable you have already defined within your function.最后,在您的if(month<10){...}条件中,不要重新定义您已经在 function 中定义的变量。 It would probably not throw an error or have any negative effect, but we can only thank the current forgiving javascript implementation for that, redefinition should not be allowed in the same scope.它可能不会引发错误或产生任何负面影响,但我们只能感谢当前宽容的 javascript 实现,在同一个 scope 中不应允许重新定义。

Finally make sure you put all your jQuery initialisation code into the jQuery ready function to make sure the DOM and jQuery itself has fully loaded.最后确保将所有 jQuery 初始化代码放入 jQuery 准备就绪 function 以确保 DOM 和 ZF590B38FDA2C30BE28DD 本身已完全加载。

And sorry for the rant... ;)并为咆哮感到抱歉... ;)

$(function(){
    var number = 0;

    $('#add_date').click(function(event) {
        number++;

        var d=new Date();
        var year = d.getFullYear();
        var day = d.getDate();
        var month = d.getMonth() + 1;

        if (month<10) month = "0"+month;
        if (day<10) day = "0"+day;
        var fullyear = month+"/"+day+"/"+year;

        // Insert @Pointy's solution in here...
    });
});

You can add a fict你可以添加一个虚构

<input type='text' id='" + num + "' name='" + num + "' value='"+ fullyear + "' size='10' class='fake-class'/>

And then, you can load the datepicker object like this:然后,您可以像这样加载日期选择器 object:

$('.fake-class').each(function(){
    $(this).datepicker();
});

Hope it helps希望能帮助到你

Just a little correction about the variable num and number, @Pointy was using num and @DarthJDG was using number.只是对变量 num 和 number 进行了一点修正,@Pointy 使用的是 num,@DarthJDG 使用的是 number。 Here is the complete code that worked for me这是对我有用的完整代码

In Script:在脚本中:

   var num = 0;
   $('#btn').click(function(event) {
        <!-- Set the default date to be todays date, can be removed for blank-->
        num++;  
        var d=new Date();
        var year = d.getFullYear();
        var day = d.getDate();
        var month = d.getMonth() + 1;

        if (month<10) month = "0"+month;
        if (day<10) day = "0"+day;
        var fullyear = month+"/"+day+"/"+year;
        <!-- End -->

        var picker = $("<input/>", {
          type: 'text',
          id: num,
          name: num,
          value: fullyear
        }).datepicker();
        $('#holder').append(num).append(picker);
    }

And in the HTML:在 HTML 中:

<input type="button" id="btn" value="button">
<div id="holder">
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM