在数据库中显示所有数据,并在Javascript中用于循环
我有一个复选框,如果选中,它将重新启用一行中的所有输入文本框。目前的状态是我只能在第一行上做 这是我的代码:在数据库中显示所有数据,并在Javascript中用于循环,javascript,php,html,laravel,laravel-8,Javascript,Php,Html,Laravel,Laravel 8,我有一个复选框,如果选中,它将重新启用一行中的所有输入文本框。目前的状态是我只能在第一行上做 这是我的代码: @foreach($kucings as $cat) <tr> <th scope="row"> <input type="hidden" name="" value=""> <input type="
@foreach($kucings as $cat)
<tr>
<th scope="row">
<input type="hidden" name="" value="">
<input type="checkbox" id="name" name="name" value="{{$cat->id}}"/>
{{$cat->name}}
</th>
<td><input type="number" id="age" value="" disabled /></td>
<td><input type="number" id="weight" value="" disabled /></td>
<td><input type="number" id="height" value="" disabled /></td>
</tr>
@endforeach
@foreach($kucings作为$cat)
{{$cat->name}
@endforeach
下面是我的javascript:
<script>
document.getElementById('name').onchange = function() {
document.getElementById('age').disabled = !this.checked;
document.getElementById('weight').disabled = !this.checked;
document.getElementById('height').disabled = !this.checked;
};
</script>
document.getElementById('name')。onchange=function(){
document.getElementById('age').disabled=!this.checked;
document.getElementById('weight').disabled=!this.checked;
document.getElementById('height').disabled=!this.checked;
};
不能有多个元素具有相同的id
。您可以像这样拥有动态id
,并为每个id添加事件侦听器
@foreach($kucings as $key => $cat)
<tr>
<th scope="row">
<input type="hidden" name="" value="">
<input type="checkbox" id="name-{{$key}}" name="name" value="{{$cat->id}}"/>
{{$cat->name}}
</th>
<td><input type="number" id="age-{{$key}}" value="" disabled /></td>
<td><input type="number" id="weight-{{$key}}" value="" disabled /></td>
<td><input type="number" id="height-{{$key}}" value="" disabled /></td>
</tr>
@endforeach
<script>
let catCount = {{count($kucings)}};
for (let i = 0; i < catCount.length; i++) {
document.getElementById('name-'+i).onchange = function() {
document.getElementById('age-'+i).disabled = !this.checked;
document.getElementById('weight-'+i).disabled = !this.checked;
document.getElementById('height-'+i).disabled = !this.checked;
}
};
</script>
@foreach($kucings as$key=>$cat)
{{$cat->name}
@endforeach
让catCount={count($kucings)};
for(设i=0;i
谢谢。。但是这个代码对我不起作用。我使用的是一个用于web的Laravel8developing@wanprogrammer您能告诉我哪些语法不受支持吗?没有错误显示,并且显示了界面。但选中该复选框时,它不会对所有文本框起作用。。我以前的代码可以在一行上运行。您的问题是双重的。您的onchange事件只与一行挂钩,因为您使用的是ID,如果您想将其应用于所有行,只需使用类即可。其次,如果您尝试提交启用了两行年龄、体重和身高的表单,则仅保存最后一行。因此,在您的名称属性中,您需要将其设置为,这对我来说太复杂了。你能告诉我解决办法吗?这已经快一周了,我试过了,我猜如果这三行都被启用的话,你的目标应该是保存这三行,对吧?但我的声明仍然是,使用类而不是ID,并使用数组名称组,这样您就可以保存多个启用的行谢谢您的建议。通过循环应用脚本需要时间,现在就开始工作。现在我正在努力将数据提交到数据库部分