返回列表 發帖

請問如何將一個dynamic產生formfield id pass入jQuery function?

小弟新近寫了一個dynamic add table的event, 當.addow button被click時, 就會clone table最後一行, 每行格內都有一個formfield, 我在後台的JSP可以用String food[]取回值, 但我不能apply已寫好的suggestbox function到這個form field, 請問該如何處理呢? 謝謝
<script type="text/javascript">        
        $(document).ready(function() {
                $('#form1').validationEngine();
                
                var newRowNum = 0;
                $('.addRow').click(function(){
                        var $newTr = $('#tb1 tbody>tr:last').clone(true);
                        $('input[id^=foods]', $newTr).val('');
                        $newTr.insertAfter('#tb1 tbody>tr:last');
                        newRowNum += 1;                                         <==這裡想做一個counter, 想它可以識別到第newRowNum個formfield  
                        $newTr.find('input:even').attr('id', function(){
                        $('input#foods'+ newRowNum).jsonSuggest(                <==這裡想pass新產生的formfield id進去, 但不成功
                                function(text, wildCard, caseSensitive, notCharacter) { 
                                        rez = $.ajax({ 
                                        type: 'GET', 
                                        url: 'getJSON.jsp',
                                        data: 'foods=' + text,
                                        dataType: 'json', 
                                        async: false 
                                });
                                return eval(rez.responseText); 
                                },
                                { ajaxResults:true 
                                });
                        });
                });
        });
</script>
已試了('#foods'+newRowNum)......等等, 都不得要領..... 但如果我直接hardcode做$('input[id=foods]').jsonSuggest( 就沒有問題, 可惜只有第一行可以出現suggestbox, 請指教, 謝謝

[ 本帖最後由 cage 於 2009-11-11 09:57 編輯 ]

謝謝, 但請經過STEP 4 bind jsonSuggest()後, 再click addrow就會將之前的屬性clone到新row上.... 產生了一個大問題

我在click了7次addrow後再輸入的所出的suggestbox結果....
<script type="text/javascript">        
        $(document).ready(function() {
                $('#form1').validationEngine();
                var newRowNum = 1;
                $(".addRow").click(function(){
                        var $newTr = $("#tb1 tbody>tr:last").clone(true);
                        $newTr.find('.jsonSuggestResults').remove();                 <======這行嘗試clear jsonSuggestResults的div
                        $newTr.appendTo("#tb1 tbody");
                        $('input[id^=foods]', $newTr).val('');
                        $newTr.find('input[id^=foods]').each( function(){
                                $(this).jsonSuggest(
                                                function(text, wildCard, caseSensitive, notCharacter) {
                                                        rez = $.ajax({ 
                                                                type: 'GET', 
                                                                url: 'getFoodJSON.jsp',
                                                                data: 'foods=' + text,
                                                                dataType: 'json', 
                                                                async: false 
                                                        });
                                                        return eval(rez.responseText); 
                                                        },
                                                        { ajaxResults:true 
                                                        });
                        });
                        $newTr.find('input[id^=supplyDate]').each('id', function(){
                                $(this).datepicker({dateFormat:'yy-mm-dd'});
                        });
                });
        });
</script>
請問有沒有方法clear之前formfield的"記錄", 或者如何unbind 11至23行的jsonSuggest function呢?..... 謝謝

[ 本帖最後由 cage 於 2009-11-11 11:27 編輯 ]

TOP

成功了, 謝謝

TOP

返回列表 回復 發帖