0

是否有理由为第一个输入激活模糊事件。但是对于动态生成的文本框,它不会触发。

<input type="text" class="year" />
<div id="t"></div>

<script>
$('.year').blur(function () {
    $('#t').html('<input type="text" id="y" />');
});

$('#y').on('blur', function () {
    alert('hello');
});
</script>

检查小提琴 http://jsfiddle.net/kynsai/6pfST/

编辑

我正在使用以下代码创建输入

<input type="text" id="rows" />
<div id="form"></div>



 $('#rows').on('blur', function () {
    msg = '<table"><tr><td>Year</td></tr>';
    for (i = 1; i <= $(this).val(); i++) {
        msg = msg + '<tr><td><input type="text" id="Year_' + i + '"></td></tr>';
    }
    msg = msg + '</table>';
    $('#form').html(msg);
});

那么为什么这条线不起作用呢?

  $('input[id ^="Year_"]').on('blur', function () {
    alert('t');

});

提前致谢

4

3 回答 3

4

使用 .on() (.live() 在新的 jQueries 中已弃用)。并将其包装在文档准备功能中。

$(document).ready(function() {
    $(document).on('focusout','.year', function() {
       $('#t').html('<input type="text" id="y" />');
    }).on('focusout', '#y', function() {
       alert('hello');
    });
});
于 2013-04-06T11:46:27.480 回答
0

尝试以下代码...使用实时功能

<script>
$('.year').live('blur',function () {
    $('#t').html('<input type="text" id="y" />');
});

</script>
于 2013-04-06T11:44:05.080 回答
0

尝试将你的模糊事件#y放在模糊事件中.year,因为它在事件之外可能不起作用,因为它还没有放在你的html中,所以试试这个代码:

$('.year').on('blur',function () {
    $('#t').html('<input type="text" id="y" />');

    $('#y').on('blur', function () {
        alert('hello');
    });
});
于 2013-04-06T11:47:54.843 回答