douzai9405 2019-08-13 04:29
浏览 45

too long

how to create the 'if-else' condition with the following ajax jquery:

<script type="text/javascript">
    $(document).ready(function () {
        $('select[name="org_id"]').on('change', function () {
            var stateID = $(this).val();
            if (stateID){
                $.ajax({
                    url: '<?php base_url();?>pasien_lama/dokter/'+stateID,
                    type: "GET",
                    dataType: "json",
                    success:function (data) {
                        $('select[name="person_id"]').empty();
                        $('select[name="person_id"]').append('<option >- Pilih Dokter -</option>');
                        $.each(data, function (key, value) {
                            $('select[name="person_id"]').append('<option value="'+ value.person_id +'">'+ value.person_nm +'</option>')
                        });
                    }
                });
            } else {
                $('select[name="person_id"]').empty();
            }
        });
    });
</script>

if not selected, the select option will return empty automatically

here is html code :

<label>Poli Tujuan</label>
                                <select class="form-control select2" name="org_id" style="width: 100%;">
                                    <option selected="selected">- Pilih Poli -</option>
                                    <?php foreach ($xocp_orgs as $nama_poli): ?>
                                    <option value="<?php echo $nama_poli['org_id']; ?>"><?php echo $nama_poli['org_nm']; ?></option>
                                    <?php endforeach; ?>
                                </select>


<label>Poli Dokter Spesialis</label>
                                <select id="imageSelector" class="form-control select2" name="person_id" style="width: 100%;" required>
                                    <option>- Pilih -</option>
                                </select>
  • 写回答

2条回答 默认 最新

  • dongxu198714 2019-08-13 05:13
    关注

    Set the value for the default option to a empty string

    <option selected="selected" value="">- Pilih Poli -</option>
    

    change your if statement to

      if (stateID.length) {//
    

    demo:

    $('select[name="org_id"]').on('change', function () {
                var stateID = $(this).val();
                if (stateID.length){
                   console.log('has values');
                } else {
                   console.log('empty');
                    $('select[name="person_id"]').empty();
                }
            });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <label>Poli Tujuan</label>
    <select class="form-control select2" name="org_id" style="width: 100%;">
      <option selected="selected" value="">- Pilih Poli -</option>
      <?php foreach ($xocp_orgs as $nama_poli): ?>
      <option value="<?php echo $nama_poli['org_id']; ?>">
        <?php echo $nama_poli['org_nm']; ?>
      </option>
      <?php endforeach; ?>
    </select>
    
    
    <label>Poli Dokter Spesialis</label>
    <select id="imageSelector" class="form-control select2" name="person_id" style="width: 100%;" required>
      <option>- Pilih -</option>
    </select>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集