2013-12-19 19 views
0

我在datepicker/autocomplete和replacewith()之間遇到了一個愚蠢的問題。我有一個表單,用戶可以添加幾行並刪除它們。爲了做出這樣的事情,我設置了一個克隆。一旦克隆完成,我做一個replaceWith()以改變輸入的名字,因爲attr函數通過用SubmitName替換名稱來使IE變得愚蠢。replaceWith()之後的日期選擇器和自動完成功能不起作用

replacewith工作正常,但datepicker函數或自動完成功能不再工作。僅供參考,這個塊就在HTML表單之後。我已經嘗試在幾個方面,如下所示,但沒有工作:

<script type="text/javascript"> 
var uniqueLeg = 1; 
$('.addLeg').click(function() { 
    var copy = $('#Leg').clone(true,true); 
    var formId = 'Leg' + window.uniqueLeg; 
    copy.attr('id', formId); 
    copy.removeAttr('style'); 

    copy.find(':input#tripdate').each(function() { 
     $(this).replaceWith("<input type='" + $(this).attr("type") + "' id='tripdate" + window.uniqueLeg + "' name='" + $(this).attr("name") + window.uniqueLeg + "' class='datepicker' />"); 
     $('.datepicker').datepicker({ dateFormat: "dd/mm/yy" }); 
    }); 
$('#trip').append(copy); 
window.uniqueLeg++; 
}); 

copy.find(':input#tripdate').each(function() { 
     $(this).replaceWith("<input type='" + $(this).attr("type") + "' id='tripdate" + window.uniqueLeg + "' name='" + $(this).attr("name") + window.uniqueLeg + "' class='datepicker' />"); 
     $(this).datepicker({ dateFormat: "dd/mm/yy" }); 
    }); 

copy.find(':input#tripdate').each(function() { 
     $(this).replaceWith("<input type='" + $(this).attr("type") + "' id='tripdate" + window.uniqueLeg + "' name='" + $(this).attr("name") + window.uniqueLeg + "' class='datepicker' />"); 
     $('tripdate'+window.uniqueLeg).datepicker({ dateFormat: "dd/mm/yy" }); 
    }); 

沒有工作。 jQuery的UI的聲明頁面

<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <TITLE> Dev page </TITLE> 
    <link rel="stylesheet" href="src/jquery-ui.css" /> 
    <script src="src/jquery-1.9.1.js"></script> 
    <script src="src/jquery-ui.js"></script> 
    <SCRIPT language="javascript"> 
$(function() { 
      $(".datepicker").datepicker({ dateFormat: "dd/mm/yy" }); 
      $(".aptsearch").autocomplete({source:'suggest_apt.php', minLength:3}); 
      $(".actsearch").autocomplete({ 
       source:'suggest_act.php', 
       minLength:3, 
       select: function (event, ui) { 
        var id = $(this).attr("id"); 
        loadAC(ui.item.value, id); 
       } 
      }); 
     });... 

除此之外的頂部,甚至addClass或RemoveClass功能不似乎工作。請指教,我看不到缺少的東西。 歡迎提出建議。新聞行之前

HTML結構的消息後,行

<INPUT id=tripdate0 class="datepicker hasDatepicker" name=tripdate0 jQuery191007588717778684739="15"> 

<INPUT id=tripdate1 class=datepicker name=tripdate1> 
+0

你是否在控制檯中得到任何錯誤?還有一件事,爲什麼你多次調用'.datepicker()',因爲你需要它來添加所有新添加的行,你可以在'$(document).ready()'中調用它一次。如果可能的話,可以在添加新行之後發佈html結構 – dreamweiver

+0

我沒有收到任何錯誤,但自動完成和datepicker在新添加的行上不起作用。 關於$(document).ready(function){它並不能代替克隆後必須再次調用.datepicker()的事實。我只是嘗試過,沒有它就沒有用。 – poypoy

回答

0

好吧,我找出了問題。 AddClass和.datepicker無法在輸入id已被replacewith更改的函數上工作。 解決這個問題的唯一方法是添加一個額外的函數,然後選擇新的修改過的輸入並添加類datepicker +初始化.datepicker()

看起來有點髒,但至少起作用。如果你有一些建議來改善這一點,請隨時分享:)

copy.find(':input#tripdate'+window.uniqueLeg).each(function() { 
     $(this).addClass('datepicker'); 
     $(this).datepicker({ dateFormat: "dd/mm/yy" }); 
    }); 
相關問題