加载运行时插入的模式的Javascript

时间:2013-09-14 07:59:19

标签: javascript

如何在加载html后执行此javascript。

<input type="text" id="rmb" name="price" class="price span1" value="2" />
<input type="text" id="sgd" />


    $('#rmb').keyup(function(){
        $('#sgd').val(Number(this.value/4.7).toFixed(2));
    });

在我的.js文件中,用于检测单击的提交按钮

dialogEditItem.one('click', '.submit', function(e){
我的jsfiddle http://jsfiddle.net/ZLr9N/

我的模态HTML

<script id="product-data" type="text/html">

        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h3><%= data.name %></h3>
        </div>
        <div class="modal-body">
            ...
        <input type="text" id="rmb" name="price" class="price span1" value="<%= data.price %>">
                        <input type="text" id="sgd" />
                    ...
        <div class="modal-footer">
            <button class="btn green submit">Submit</button>
            <button class="btn red cancel" data-dismiss="modal">Cancel</button>
        </div>
    </script>

1 个答案:

答案 0 :(得分:1)

您尝试将处理程序绑定到输入元素,然后才能成为DOM的一部分:

$('#rmb').keyup(handlerFn); // when #rmb is not present yet the jQuery object will be empty and no handler will be bound

而是使用事件委托,将处理程序绑定到始终存在的document对象:

$(document).on('keyup', '#rmb', handlerFn); //document will always be present, no matter if #rmb is added to the DOM lateron, so binding will be successful

请参阅.on()上的 the docs 和委派。