2014-10-08 64 views
0

HTMLJavaScript函數只能使用一次。任何想法爲什麼?

我正在嘗試創建一個todolist應用程序。我想讓每個任務列表都用漸變過渡來刪除。我已經實現了它,但是我的代碼的問題是它只隱藏了第一個div區域。

<div id="task"> 
      <input type="checkbox" id="completed"> 
      <h2>Buy some fruits</h2> 
     </div> 
     <div id="task"> 
      <input type="checkbox" id="completed"> 
      <h2>Buy some fruits</h2> 
     </div> 

JQuery的

$("#completed").change(function(){$("#item").fadeToggle("fast", "linear")}); 
+7

你有在網頁上使用相同ID不止一個元素

$(".completed").change(function(){ } $(this).hide();}); 

請修改此代碼。 – akonsu 2014-10-08 05:34:58

+0

您應該使用'''class'''而不是''ID''',因爲Id在上下文中應該是唯一的,並且只返回具有相同'''ID''的第一個元素 – kuldipem 2014-10-08 05:37:36

回答

5

不使用mutipple id's在同一頁上使用class代替

<div class="task"> 
    <input type="checkbox" class="completed"> 
    <h2>Buy some fruits</h2> 
</div> 
<div class="task"> 
    <input type="checkbox" class="completed"> 
    <h2>Buy some fruits</h2> 
</div> 

$(".completed").change(function(){$("#item").fadeToggle("fast", "linear")}); 

// same apply to your #item 
0

你不應該具有相同id多個元素。這個屬性的全部要點是唯一標識一個元素。因此

<input type="checkbox" class="completed"> 

而JavaScript:

喜歡的東西取代它

$(".completed").change(function(){$("#item").fadeToggle("fast", "linear")}); 

這應該整理你的問題了。

0

試試這個:

<div> 
    <input type="checkbox" class="checkbox"> 
    <h2>Buy some fruits</h2> 
</div> 
<div> 
    <input type="checkbox" class="checkbox"> 
    <h2>Buy some fruits</h2> 
</div> 
<script type="text/javascript"> 
    $(".checkbox").change(function(){$(this).parent().fadeToggle("fast", "linear")}); 
</script> 

OR

<div> 
    <input type="checkbox"> 
    <h2>Buy some fruits</h2> 
</div> 
<div> 
    <input type="checkbox"> 
    <h2>Buy some fruits</h2> 
</div> 
<script type="text/javascript"> 
    $("input[type=checkbox]").change(function(){$(this).parent().fadeToggle("fast", "linear")}); 
</script> 
0

id兩個輸入元素相同。

您可以使用班級而不是id

像下面爲每U [R

相關問題