我有一个从数据库获取的数据列表,我想在选中复选框上启用输入字段。然而它只与第一行一起工作。这是HTML代码:
<?php
require_once('inc/config.php');
include 'verification/verify_form_details.php';
?>
<html>
<head>
<title>getElementById example</title>
<script type="text/javascript" src="js/jquery-2.1.3.min.js"></script>
</head>
<body>
<?php
$result2 = getProcessID();
?>
<label>Process: <small style="color:red;">*</small>
<?php
while ($row = mysql_fetch_array($result2))
{
$row[0] = cleanOutputData($row[0]);
?>
<div>
<label>
<input type="checkbox" class="checkbox" name="process[]" id=<?php echo $row[0] ?> value=<?php echo $row[0]?> /><?php echo $row[0] ?>
</label>
<label>
<input type="text" class="field" disabled name="number[]" id=<?php echo $row[0] ?> />
</label>
</div>
<?php
}
mysql_free_result($result2);
?>
</label>
</body>
</html>
和javascript功能:
<script>
$(document).ready(function () {
$('.checkbox').change(function() {
$('.field').attr('disabled',!this.checked)
});
});
</script>
我有什么想法可以做到吗?谢谢
答案 0 :(得分:4)
$('.checkbox').change(function() {
$(this).parent().next().find('.field').prop('disabled', !$(this).is(':checked'))
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
<input type="checkbox" class="checkbox" name="process[]" id=v alue=/>
</label>
<label>
<input type="text" class="field" disabled name="number[]" id=/>
</label>
<label>
<input type="checkbox" class="checkbox" name="process[]" id=v alue=/>
</label>
<label>
<input type="text" class="field" disabled name="number[]" id=/>
</label>
<label>
<input type="checkbox" class="checkbox" name="process[]" id=v alue=/>
</label>
<label>
<input type="text" class="field" disabled name="number[]" id=/>
</label>
&#13;