动态创建的文本框上的 jQuery 按键事件
我正在动态创建一个表,并希望捕获动态创建的日期文本框上的按键事件。如果我只有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然
jQuery.live()
可以工作,但最好不要以这种方式进行 JavaScript 开发。在发出
元素时,添加“ payment_date”作为类。现在,说它
的优点是它对于任意数量的具有 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, sayThe advantage of this is that it'll work fine for any number of divs that have the payment_date class.
使用
jQuery.live()
它将函数添加到 DOM 结构中were
、are
和will be
的元素选择器http://api.jquery.com/live/
use
jQuery.live()
it adds functions to element selectors whichwere
,are
andwill be
in DOM structurehttp://api.jquery.com/live/
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.