动态创建的文本框上的 jQuery 按键事件

发布于 2024-12-04 12:38:07 字数 1308 浏览 1 评论 0原文

我正在动态创建一个表,并希望捕获动态创建的日期文本框上的按键事件。如果我只有一个 payment_date 文本框,那很好,但我有很多。我看过 .live 但我对此还很陌生,无法弄清楚。有人可以具体给我吗?以下是包含相关文本框的 HTML:

        <?php 
        foreach($student_classes as $class):
            if( $student['student_id'] == $class['student_id'] ) {                
                $i = $class['registration_id'];
                $deleted = FALSE;
        ?>
             <td>
                <input type="text" 
                name="students[<?php echo $i ?>][registration_payment_date]"
                id="payment_date[<?php echo $i ?>]" 
                value="<?= html($class['registration_payment_date']) ?>"
                size='10' maxlength="10" 
                > 
            </td>
       <?php endforeach; ?>

jQuery:

jQuery(document).ready(function ()
{
       var $payment_date = jQuery('#payment_date');    
       // Format the date as it is entered
          $payment_date.keypress(function(event) {
          DateFormat(this, this.value, event, false, '1');
      });

     // Check to make sure the date is valid
        $payment_date.change(function ()
    {
        if( dateValid(this.value) == false );   
        alert('The date is not valid'); 
    });
}); 

谢谢!

I am dynamically creating a table and want to capture the keypress event on the dynamically created date textboxes. If I have only one payment_date textkbox it's great, but I have many. I've looked at .live but I'm too new at this to figure it out. Can someone be specific for me please? Here is the HTML with the textbox in question:

        <?php 
        foreach($student_classes as $class):
            if( $student['student_id'] == $class['student_id'] ) {                
                $i = $class['registration_id'];
                $deleted = FALSE;
        ?>
             <td>
                <input type="text" 
                name="students[<?php echo $i ?>][registration_payment_date]"
                id="payment_date[<?php echo $i ?>]" 
                value="<?= html($class['registration_payment_date']) ?>"
                size='10' maxlength="10" 
                > 
            </td>
       <?php endforeach; ?>

jQuery:

jQuery(document).ready(function ()
{
       var $payment_date = jQuery('#payment_date');    
       // Format the date as it is entered
          $payment_date.keypress(function(event) {
          DateFormat(this, this.value, event, false, '1');
      });

     // Check to make sure the date is valid
        $payment_date.change(function ()
    {
        if( dateValid(this.value) == false );   
        alert('The date is not valid'); 
    });
}); 

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等待圉鍢 2024-12-11 12:38:07

虽然 jQuery.live() 可以工作,但最好不要以这种方式进行 JavaScript 开发。

在发出 元素时,添加“ payment_date”作为类。现在,说它

$(".payment_date").bind("keypress", function(event){
    DateFormat(this, this.value, event, false, '1');
}

的优点是它对于任意数量的具有 payment_date 类的 div 都可以正常工作。

While jQuery.live() will work, it's best not to approach JavaScript development this way.

While emitting the <input> element, add "payment_date" as a class. Now, say

$(".payment_date").bind("keypress", function(event){
    DateFormat(this, this.value, event, false, '1');
}

The advantage of this is that it'll work fine for any number of divs that have the payment_date class.

作业与我同在 2024-12-11 12:38:07

使用 jQuery.live() 它将函数添加到 DOM 结构中 werearewill be 的元素选择器

http://api.jquery.com/live/

var $payment_date = jQuery('#payment_date');    
   // Format the date as it is entered
  $payment_date.live("keypress",function(event) {
      DateFormat(this, this.value, event, false, '1');
  });

 // Check to make sure the date is valid
    $payment_date.live("change",function ()
{
    if( dateValid(this.value) == false );   
    alert('The date is not valid'); 
});

use jQuery.live() it adds functions to element selectors which were,are and will be in DOM structure

http://api.jquery.com/live/

var $payment_date = jQuery('#payment_date');    
   // Format the date as it is entered
  $payment_date.live("keypress",function(event) {
      DateFormat(this, this.value, event, false, '1');
  });

 // Check to make sure the date is valid
    $payment_date.live("change",function ()
{
    if( dateValid(this.value) == false );   
    alert('The date is not valid'); 
});
不再见 2024-12-11 12:38:07

JQuery live() 方法现已弃用,并从 1.9 中删除。

您应该使用 on() 方法将事件绑定到动态创建的元素。

JQuery live() method is deprecated now, and removed from 1.9.

You should use on() method to bind event to dynamically created elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文