开发者

problem in run some js codes after click

why after search and click on result search and click on plus(button add input) in the example on part Adding input, Date formating($('.find_input').delegate('input.date:text', 'keyup', ....) and normal number formatting($('.find_input').delegate('input.numeric:text','keyup',...) not work.

EXAMPLE: http://www.binboy.gigfa.com/admin/tour_foreign/insert_foreign

Js full code: http://jsfiddle.net/ZpDDR/

...
///// Date formating /////////////////////////////////////////////////////////////////////////////////////////
    $dateSet = 0;
    $monthSet = 0;
    $yearSet = 0;

    $('.find_input').delegate('input.date:text', 'keyup', function () {
        $val = $(this).val().replace(/[^\d]+/g, "").match(/\d{1,12}$/);
        if($val == null) {
            return;
        } else {
            $val = $val.join("");
        }

        if($(this).val().match(/\d{4,}$/) && $val.length%2 == 0) {
            $val = $val.match(/\d{2}/g);
            if($yearSet < $monthSet) {
                if($val.length == 4) {
                    $(this).val($val.join("").replace(/(\d{2})(\d{2})(\d{4})$/,'$3/$1/$2'));
                    $yearSet++;
                } else if($val.length == 6){

                    $(this).val($val.join("").replace(/(\d{4})(\d{2})(\d{2})(\d{4})$/,'$4/$2/$3'));
                    $yearSet++;
                }
            } else {
                if($monthSet < $dateSet) {
                    $(this).val($val.join("").replace(/(\d{4})(\d{2})(\d{2})(\d{2})$/,'$1/$4/$3'));
                    $monthSet++;
                } else {

                    if($val.length == 2) {
                           $(this).val($val.reverse().join("/"));
                        $dateSet++;
                        $monthSet++;
                    } else {
                           $(this).val($val.join("").replace(/(\d{4})(\d{2})(\d{2})(\d{2})$/,'$1/$2/$4'));
                        $dateSet++;
                    }
       开发者_Python百科         }
            }
        }
    });

///normal number formatting/////////////////////////////////////////////////////////////////////////////////////////    
    $('.find_input').delegate('input.numeric:text','keyup',function () {
        $val = $(this).val().match(/[0-9]/g).reverse().join("").match(/[0-9]{1,3}/g).join(",").match(/./g).reverse().join("");
        $(this).val($val)
    });


The problem is that the element that you're delegating from,.find_input, doesn't exist yet when you're setting up the delegation function. delegate allows for defining event handlers for elements that aren't created yet, but only elements that match the second selector E.g., $('#must-exist-now').delegate('.can-be-created-later', ...);

I'm not sure if I described that well, but the solution is to change your statement to delegate from something that already exists on DOM load. For example: $(document).delegate('input.date:text', ...).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜