2013-02-19 188 views
1

我目前正在設計一個使用javascript,html和css的網頁。該頁面是座位預訂座位,允許用戶選擇和取消選擇座位。當用戶選擇座位時,我設法讓頁面顯示座位的ID,但是如果用戶取消選擇座位,我在嘗試從顯示中刪除ID時遇到問題。附加並刪除元素

下面是JavaScript代碼

$('.available, .unavailable, .selected').click(function(){ 
     var ID = $(this).attr('class'); 
     if (ID == 'n unavailable' || ID == 'p unavailable') { 
      alert ('Seat is already booked. Please select another seat.'); 
     } 
     else if (ID == 'n selected' || ID == 'p selected') { 
      alert ('You have now unselected this seat.'); 
      $(this).html('<img src = "free.gif"/>'); 
      $(this).removeClass('selected').addClass('available'); 
      y--; 
      $("#seats").html("Number of seats selected: " + y); 
      $("#list").remove($(this).attr('id')); 
     } 
     else { 
      alert ('You have now reserved this seat. You can unselect it by clicking the seat again.'); 
      $(this).html('<img src = "selected.gif"/>'); 
      $(this).removeClass('available').addClass('selected'); 
      y++; 
      $("#seats").html("Number of seats selected: " + y); 
      $("#list").append($(this).attr('id') + "</br>"); 
     } 
    }); 
+0

什麼是'$( '#list')'?它是一個顯示所選座位ID的'div'或'span'嗎? – Marcus 2013-02-19 21:23:00

回答

1

remove()刪除一個DOM元素,而不是一個屬性。就在屬性設置爲空字符串:$("#list").attr('id', '')

編輯

審查你的問題後,我想我誤解了。你想刪除顯示ID的文本,而不是實際的ID屬性,對嗎?

在這種情況下,最簡單的事情就是將文本節點包裝在某種元素中,以便更容易地選擇刪除。

在那裏你將字符串($("#list").append($(this).attr('id') + "</br>");),包裹輸出跨度像這樣:

$("#list").append('<div class="id">' + $(this).attr('id') + "</div>"); 

這樣,您就可以刪除它是這樣:

$('#list').remove('#id');