Php 如果选中jQuery,则启用动态输入文本
我正在使用PHP生成一个包含表单的表,例如:Php 如果选中jQuery,则启用动态输入文本,php,jquery,html,Php,Jquery,Html,我正在使用PHP生成一个包含表单的表,例如: <table class="table table-hover"> <thead> <tr> <th class="table-checkbox"> </th> <th> Description </th>
<table class="table table-hover">
<thead>
<tr>
<th class="table-checkbox">
</th>
<th>
Description
</th>
<th>
Qty
</th>
<th>
Input Qty
</th>
</tr>
</thead>
<tbody>
<tr class="grade odd" role="row">
<td class="verti-align">
<label>
<div class="checker" id="uniform-goods[]"><span><input type="checkbox" class="goods[]" value="13" name="goods[]" id="goods[]" style="cursor:pointer;" data-title="Bump" data-set="#goods_table .goods"></span></div>
</label>
</td>
<td class="verti-align sorting_1">
Bump
</td>
<td class="verti-align">
76
</td>
<td>
<input type="text" name="qty[]" id="qty[]" class="form-control input-sm" value="76" disabled="disabled">
</td>
</tr>
<tr class="grade even" role="row">
<td class="verti-align">
<label>
<div class="checker" id="uniform-goods[]"><span><input type="checkbox" class="goods[]" value="17" name="goods[]" id="goods[]" style="cursor:pointer;" data-title="Durms" data-set="#goods_table .goods"></span></div>
</label>
</td>
<td class="verti-align sorting_1">
Durms
</td>
<td class="verti-align">
889
</td>
<td>
<input type="text" name="qty[]" id="qty[]" class="form-control input-sm" value="889" disabled="disabled">
</td>
</tr>
<tr class="grade odd" role="row">
<td class="verti-align">
<label>
<div class="checker" id="uniform-goods[]"><span><input type="checkbox" class="goods[]" value="16" name="goods[]" id="goods[]" style="cursor:pointer;" data-title="Guitar" data-set="#goods_table .goods"></span></div>
</label>
</td>
<td class="verti-align sorting_1">
Guitar
</td>
<td class="verti-align">
87
</td>
<td>
<input type="text" name="qty[]" id="qty[]" class="form-control input-sm" value="87" disabled="disabled">
</td>
</tr>
</tbody>
</table>
jQuery脚本:
<?php foreach ($goodswo as $good):?>
<tr class="odd grade">
<td class="verti-align">
<label>
<input type="checkbox" class="goods[]" value="<?php echo htmlspecialchars($good->id,ENT_QUOTES,'UTF-8');?>" name="goods[]" id="goods[]" style="cursor:pointer;" data-title="<?php echo htmlspecialchars($good->description,ENT_QUOTES,'UTF-8');?>" data-set="#goods_table .goods"/>
</label>
</td>
<td class="verti-align">
<?php echo htmlspecialchars($good->description,ENT_QUOTES,'UTF-8');?>
</td>
<td class="verti-align">
<?php echo htmlspecialchars($good->qty,ENT_QUOTES,'UTF-8');?>
</td>
<td>
<input type="text" name="qty[]" id="qty[]" class="form-control input-sm" value="<?php echo htmlspecialchars($good->qty,ENT_QUOTES,'UTF-8');?>" disabled="disabled">
</td>
</tr>
<?php endforeach;?>
$(document).ready(function() {
$('table input[name="goods[]"]').change(function() {
$('input[name="qty[]"]').attr('disabled', !this.checked)
});
});
尝试向上遍历
(使用父项()
),然后再次向下遍历(使用查找()
)。使用$(this)
标记从何处开始。您不需要使用任何唯一的id
或您拥有的东西。实际上,您无需对代码进行太多更改,即可使其按预期工作:
$(document).ready(function() {
$('table input[name="goods[]"]').change(function() {
$(this).parents("tr").find('input[name="qty[]"]').attr('disabled', !this.checked);
});
});
小提琴:
<?php foreach ($goodswo as $good):?>
<tr class="odd grade">
<td class="verti-align">
<label>
<input type="checkbox" class="goods[]" value="<?php echo htmlspecialchars($good->id,ENT_QUOTES,'UTF-8');?>" name="goods[]" id="goods[]" style="cursor:pointer;" data-title="<?php echo htmlspecialchars($good->description,ENT_QUOTES,'UTF-8');?>" data-set="#goods_table .goods"/>
</label>
</td>
<td class="verti-align">
<?php echo htmlspecialchars($good->description,ENT_QUOTES,'UTF-8');?>
</td>
<td class="verti-align">
<?php echo htmlspecialchars($good->qty,ENT_QUOTES,'UTF-8');?>
</td>
<td>
<input type="text" name="qty[]" id="qty[]" class="form-control input-sm" value="<?php echo htmlspecialchars($good->qty,ENT_QUOTES,'UTF-8');?>" disabled="disabled">
</td>
</tr>
<?php endforeach;?>
$(document).ready(function() {
$('table input[name="goods[]"]').change(function() {
$('input[name="qty[]"]').attr('disabled', !this.checked)
});
});
谢谢你清楚的解释,这很有帮助。