2015-09-04 56 views
1

我想創建一個包含問題,複選框和文本框的表格。所有的問題都在數據庫中,我將它們稱爲數組。然後,我創建一個禁用文本框,並在複選框被選中時啓用它。使用數據庫中的所有數據陣列,如何使用enable disable字段創建複選框?

所以問題是,我怎樣才能使所有的複選框和文本框使用數組中的腳本?因爲它似乎只適用於第一行,爲什麼?

$(function() { 
    $('#checkservice<?php $ID ?>').click(function() { 
     $('#txtpercent<?php $ID ?>').prop('disabled', !this.checked); 
    }); 
}); 

以上是我的javascript。這工作完美。

<?php 
$query1 = mysql_query("SELECT * FROM ftrquestion ORDER BY ID") or die("could not search!"); 
    $count1 = mysql_num_rows($query1); 

    if ($count1 = 0){ 
     $output = 'Theres was not search result !'; 
     } else { 
     while ($row1 = mysql_fetch_array($query1)) { 
      $ID = $row1['ID']; 
      $MCPA = $row1['MCPA']; 
      $quesDescBM = $row1['quesDescBM']; 
      $quesDescBI = $row1['quesDescBI']; 
      ?> 

      <tr height="55px"> 
      <td ><center><?php echo $ID ?></center></td> 
      <td ><?php echo $MCPA ?></td> 
      <td ><span class="BM"><?php echo $quesDescBM ?> </span> <span class="BI"><?php echo $quesDescBI ?> </span></td> 
      <td ><a href=#><img src="images.png"></td> 
      <td><input type="checkbox" id="checkservice<?php $ID ?>" name="checkservice>"></td> 
      <td><input type="text" id="txtpercent<?php $ID ?>" name="txtpercent" size="1" maxlength="3" disabled> </td> 
      </tr> 
      <?php 
      } 
     } 
?> 
+1

這是JavaScript ....... eRedz

回答

0
$(function() { 
    $('#checkservice<?php $ID ?>').on('click', function() { 
     $('#txtpercent<?php $ID ?>').prop('disabled', !this.checked); 
    }); 
}); 

也許元素不會在您添加的點擊功能的時候存在嗎?您可以使用on()方法添加DOM中不存在的函數。你可以閱讀有關委派事件在這裏:http://api.jquery.com/on/

編輯

您需要點擊功能添加到每個元素,讓你無論是在PHP創建一個循環,併爲每個ID創建一個功能或您添加類添加到您的輸入並將該函數添加到該類。

$(function() { 
    $('.checkservice').on('click', function() { 
     $(this).parent().next().children('input').prop('disabled', !this.checked); 
    }); 
}); 

<td><input type="checkbox" id="checkservice<?php $ID ?>" class="checkservice" name="checkservice>"></td> 
相關問題