1

How do i execute this javascript only after the html is loaded.

<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));
    });

inside my .js file , used to detect for submit button clicked

dialogEditItem.one('click', '.submit', function(e){

my jsfiddle http://jsfiddle.net/ZLr9N/

my modal 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>
4

1 回答 1

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()

于 2013-09-14T10:10:03.600 回答