2014-09-10 68 views
-4

我有克隆HTML數據在這裏我需要刪除一條記錄,然後我需要從該取消記錄獲得ID。 這裏是我的html代碼:如何獲得克隆HTML隱藏ID,而點擊取消按鈕

<div id="Allergies" class="clone"> 
     <div class="copy"> 
      <input type="hidden" id="allergyId" value=""> 
     /*rest of the code will be the html data*/ 
     <a class="addallergy" id="addallergy">Save and Add Allergy</a> 
     <a class="removeallergy" id="removeallergy">Cancel</a> 
     </div> 
    <div> 

這裏是我的javascript代碼:

var p=$('.copy').length; 
    var h=0; 
     for(var i=0 ; i<data.allergies.length ; i++){ 
      var cloned = $(".copy:first").clone(true) 
       .  .appendTo('#Allergies').addClass("childAllergyClass" + (i + 1)); 
      $(".childAllergyClass"+ ++h+" #allergyId").val(data.allergies[i].allergyId); 
    } 

現在我的HTML代碼將是:

<div id="Allergies" class="clone"> 
     <div class="copy childAllergyClass1"> 
      <input type="hidden" id="allergyId" value="123"> 
     /*rest of the code will be the html data*/ 
      <a class="addallergy" id="addallergy">Save and Add Allergy</a> 
      <a class="removeallergy" id="removeallergy">Cancel</a>    
     </div> 
     <div class="copy childAllergyClass2"> 
      <input type="hidden" id="allergyId" value="124"> 
     /*rest of the code will be the html data*/ 
      <a class="addallergy" id="addallergy">Save and Add Allergy</a> 
      <a class="removeallergy" id="removeallergy">Cancel</a>  
     </div> 
    <div> 

如果我們有長度爲2個那麼雙過敏顯示在UI中。如果我點擊取消,當前的記錄將通過使用代碼消失

$(".removeallergy").click(function(e) { 
     $(this).closest(".copy").fadeOut("slow", function(){ 
      $(this).remove(); 
      }); 
    }); 

然後我需要特定的記錄ID爲非活動狀態數據庫中的當前記錄狀態。

請幫助我如何從取消記錄中獲得id或其他任何更好的想法來使數據庫中的記錄無效。

+4

題外話:這是「生命威脅* en * ing「。 – 2014-09-10 13:02:20

+0

你的問題到底是什麼? – 2014-09-10 13:04:49

+0

你有兩個輸入元素具有相同的ID? – chridam 2014-09-10 13:05:08

回答

0

我得到了一個答案:在我的項目中,我使用了類似的ID和我的應用程序工作正常。

$(".removeallergy").click(function(e) { 
    $(this).closest(".copy").fadeOut("slow", function(){ 
     var abc = $("#allergyId",this).val(); 
      $(this).remove(); 
     }); 
}); 
0

這裏的IAM使用類似的ID和獲取數據successfully.please檢查下面的代碼

<div> 
    <div class="duplicate1"> 
     Name1:<input type="text" name="duplicateName" id="duplicateName"/> 
     Id1:<input type="text" name="duplicateId" id="duplicateId"/> 
    </div> 
     <div class="duplicate2"> 
     Name2:<input type="text" name="duplicateName" id="duplicateName"/> 
     Id2:<input type="text" name="duplicateId" id="duplicateId"/> 
     </div> 
    <button type="button" onclick="javascript:saveDuplicates();">Save</button> 
</div> 

這裏是Java腳本代碼:

function saveDuplicates(){ 
     var duplicate = { 
       duplicateName1 : $(".duplicate1 #duplicateName").val(), 
       duplicateId1 : $(".duplicate1 #duplicateId").val(), 
       duplicateName2 : $(".duplicate2 #duplicateName").val(), 
       duplicateId2 : $(".duplicate2 #duplicateId").val() 
     }; 
     var jsonString = JSON.stringify(duplicate); 
     alert(jsonString); 
    }