2013-02-28 35 views
0

我无法删除在切换中的第一次点击创建的跨度,并在第二次点击切换时删除。使用第二个切换删除第一个切换中实现的跨度

$(document).ready(function(){ 
$("td").toggle(
    function(){ 
     var current = $("input[name=prices]:checked").val(); 
     $(this).html("<img src='images/1.gif'/>"); 
     $(".summary").append("<br><span class = 'newticket' id = 'null'>"); 
     $("#null").attr("id",$(this).attr("id")); 
     $(".summary").append("1 x "); 
     if(current == 'full'){ 
      $(".summary").append("full price ticket."); 
     } 
     $(".summary").append(" Seat: "); 
     $(".summary").append($(this).attr("id")); 
     if(current == 'full'){ 
      $(".summary").append(" Price: £10</span>"); 
     } 

    }, 
    function(){ 
     $(this).html("<img src='images/a.gif'/>"); 
     $("span#" + $(this).attr("id")).remove(); 
}); 
}); 

如果我点击,如果我的跨度不会删除表中的值再次单击跨度但是添加的表值。

任何帮助,将不胜感激。

+0

当对DOM上不存在的元素采取行动时,您需要使用on():http://api.jquery.com/on/ – isherwood 2013-02-28 18:52:12

回答

0

你不应该追加未封闭标签 创建一个字符串然后附加它。 完整的解决方案。

$(document).ready(function(){ 

    $("td").toggle(
     function(){ 
      var current = $("input[name=prices]:checked").val(); 
      $(this).html("<img src='images/1.gif'/>"); 
      var html = "<br><span class = 'newticket' id = 'null'>"; 
      htnl += "1 x "; 
      if(current == 'full'){ 
       html +="full price ticket."; 
      } 
      html +=" Seat: " + $(this).attr("id"); 
      $(".summary").append($(this).attr("id")); 
      if(current == 'full'){ 
       html +=" Price: £10" 
      } 

      html += "</span>"; 
      $(".summary") .append(html); 
     }, 
     function(){ 
      $(this).html("<img src='images/a.gif'/>"); 
      $(".summary span.newticket").remove(); 
    }); 

});