0

我正在开发 MVC 3 应用程序,我设法使用部分视图创建了一个动态创建的文本框,并对这些动态框进行了验证,但是,我想检查这些动态文本框是否已更改并将隐藏字段值更改为“脏”因此,我使用以下代码来检测文本框上的任何更改,并且它在任何已经存在的文本框上都可以正常工作,但它不适用于动态创建的文本框:

<script type="text/javascript">
    $(document).ready(function () {
        $("input[type='text']").change(function () {
            $("#FormState").val('dirty');
        });
    });
</script>

无论如何用javascript来做到这一点?

4

2 回答 2

3

What you want to do is called event delegation. jQuery currently offers three ways to do event delegation, with live, delegate, and on.

The other answer suggested using live, but it and delegate are deprecated for good reason. They have some performance problems. You should use on for event delegation. In order to use it best you need to give it a reasonable parent element of these inputs. Try to choose the closest parent to all of your inputs.

$('#parent-id').on('change', 'input[type="text"]', function () {
    $("#FormState").val('dirty');
});
于 2012-11-22T14:55:50.097 回答
2

利用:

   $("input[type='text']").live('change',function () {
        $("#FormState").val('dirty');
    });

jQuery 直播

于 2012-11-22T14:52:04.323 回答