首页 > 解决方案 > 选择不是在 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>

标签: jquery

解决方案


使用 id 可以缩短它。

<input type="hidden" name="exam_id" id="exam_id" value=""/> 

$(document).ready(function(){
        $('select').change(function(){
            $('#exam_id').val($('#exam_term).val())  ;  
        });
});

推荐阅读