jquery - 选择不是在 jquery 中选择值
问题描述
我正在循环中进行选择下拉菜单,并将所选值放入表单隐藏属性中。它第一次正常工作,但在重新加载同一页面时,它始终为该属性设置 0。这是我的代码。这似乎很简单,但我无法理解我哪里出错了。
<?php
$dex=0;
if($row!=''){
foreach($teacher as $value){
?>
<tr>
<td><?php echo $value['first_name'].' '.$value['last_name'] ?></td>
<td><?php echo $row[$dex]['subject_name'] ?></td>
<td><?php //echo $value['father_name'] ?></td>
<td>
<div class="form-label-group">
<select id="exam_term" class="form-control" name="exam_term" >
<option value="0">Assesment</option>
<option value="1">First</option>
<option value="2">Second </option>
<option value="3">Mid Term </option>
<option value="4">Prelium </option>
</select>
</div>
</td>
<td>
<form method="post" action="<?php echo base_url()?>index.php/teacher/view_subject_marks">
<input type="hidden" name="exam_id" value=""/>
<input type="hidden" name="teacher_id" value="<?php echo $value['id'] ?>"/>
<input type="hidden" name="class_id" value="<?php echo $row[$dex]['class_id'] ?>"/>
<input type="hidden" name="subject_id" value="<?php echo $row[$dex]['subject_id'] ?>"/>
<input class="btn btn-primary btn-sm submit_btn" type="submit" value="View Marks" />
</form>
</td>
</tr>
<?php
$dex++;
}
}
?>
</tbody>
</table>
</div>
</div>
</div>
这是我的jQuery
<script>
$(document).ready(function(){
$('select').change(function(){
var exam_term = $('#exam_term option:selected').val();
alert(exam_term);
$('input[name=exam_id]').val(exam_term) ;
});
});
</script>
解决方案
使用 id 可以缩短它。
<input type="hidden" name="exam_id" id="exam_id" value=""/>
$(document).ready(function(){
$('select').change(function(){
$('#exam_id').val($('#exam_term).val()) ;
});
});
推荐阅读
- mysql - 一次发出多个sql更新语句
- c++ - 使用 C++ 将命令行参数拆分为参数/向量
- c++ - C ++在字符串中添加空格
- azure-devops - 增量计划查询
- python - 通过任务调度程序执行 Qt 应用程序时,如何加载托盘图标?
- amazon-web-services - 如何在管道中运行脚本?
- c# - 实例化从 xml 序列化的所有类对象
- javascript - SyntaxError:位置 0 处 JSON 中的意外标记 I
- testing - 测试结果查看器(Azure DevOps Pipeline)的错误/中止测试
- javascript - 试图找到由对象组成的数组的索引值