Py学习  »  Jquery

用jquery自动选择

MAHBUBUR RAHMAN • 5 年前 • 1263 次点击  

HTML代码:

        <div class="per-input per-businessHour">
                    <select name="closing_time[]" class="closing_time close_time" id="closing_time1">
                      <option value="">Choose</option>
                      @if ($range = range(strtotime('00:00'),strtotime('23:59'),15*60))
                        @foreach ($range as $time)
                          <option value="{{ date('h:i A',$time) }}">{{ date('h:i A',$time) }}</option>
                        @endforeach
                      @endif
                    </select>
                    <span value="1" class="editTime">Edit</span>
                  </div>
        <div class="per-input per-businessHour">
                    <select name="closing_time[]" class="closing_time close_time" id="closing_time2">
                      <option value="">Choose</option>
                      @if ($range = range(strtotime('00:00'),strtotime('23:59'),15*60))
                        @foreach ($range as $time)
                          <option value="{{ date('h:i A',$time) }}">{{ date('h:i A',$time) }}</option>
                        @endforeach
                      @endif
                    </select>
                    <span value="2" class="editTime">Edit</span>
                  </div>
        <div class="per-input per-businessHour">
                    <select name="closing_time[]" class="closing_time close_time" id="closing_time3">
                      <option value="">Choose</option>
                      @if ($range = range(strtotime('00:00'),strtotime('23:59'),15*60))
                        @foreach ($range as $time)
                          <option value="{{ date('h:i A',$time) }}">{{ date('h:i A',$time) }}</option>
                        @endforeach
                      @endif
                    </select>
                    <span value="3" class="editTime">Edit</span>
                  </div>

jquery代码:


    $('.closing_time').on('change', function () {
      for (var i = 1; i <= 3; i++) {
        $('#closing_time'+i).find('option[value="'+this.value+'"]').attr("selected",true);
      }
    })

    $('.editTime').on('click', function () {
      var id = $(this).attr('value');
      $('#closing_time'+id).removeClass('close_time');
      $('#closing_time'+id).removeClass('closing_time');
      $('#closing_time'+id).addClass('editclose_time');
      $('#closing_time'+id).removeAttr('id');
      $('#closing_time'+id).find('option[value=""]').attr("selected",false);
    })
    $('.editclose_time').on('change', function () {
      $('.editclose_time').find('option[value="'+this.value+'"]').attr("selected",true);
    })

我试着这样做: 如果我选择其中一个全部选择值,则所有选择选项值都将选择为相同的值。在特殊情况下,如果我尝试编辑特定的选择选项,只希望更改此选择值和所选选项。不是所有这些。但未能做到这一点…它更改了所有的选择值和选择的选项。如果有人帮我,请

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38685
 
1263 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Barmar
Reply   •   1 楼
Barmar    6 年前

这应该有效:

$(".per-businessHour").on("change", ".closing_time", function() {
    $(".closing_time").val(this.value);
});

这将使用事件委托,因此动态更改类会更改事件处理程序是否运行。单击“编辑”按钮时,它将删除 closing_time 类,因此当您从中选择时,此代码将不会运行。它还使用该类查找要更新的其他选择。