代码之家  ›  专栏  ›  技术社区  ›  Mehdi Souregi

时间输入向上箭头键向上

  •  1
  • Mehdi Souregi  · 技术社区  · 8 年前

    我希望#End的值始终等于#Start的值,即使有人只在#Start上执行keyup操作而没有释放鼠标

    // method 1 : this works only with on change not with keyup
    $('#start').change(function() {
        $('#end').val($('#start').val())
    })
    
    // method 2 : this does not work with keyup at all, donno why
    $(document).on('keyup', '#start', function() {
        $('#end').val($('#start').val())
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <input type="time" id="start" value="00:00">
    <input type="time" id="end" value="00:00">

    enter image description here

    怎么可能呢?

    2 回复  |  直到 8 年前
        1
  •  2
  •   flx    8 年前

    使用 input 事件或绑定您希望它侦听的所有事件(相当于输入事件,但我认为其跨浏览器更可靠):

    //input event
    $(document).on('input', '#start', function() {
        $('#end').val($('#start').val())
    });
    
    //all events
    $(document).on('change keyup keydown keypress click blur', '#start1', function() {
        $('#end1').val($('#start1').val())
    });
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
        <input type="time" id="start" value="00:00">
        <input type="time" id="end" value="00:00">
        <br><br>
        <input type="time" id="start1" value="00:00">
        <input type="time" id="end1" value="00:00">

    我更喜欢 输入 事件,因为它会在每次更改时更新,但正如我所说,跨浏览器。。。

        2
  •  0
  •   Kashyap    8 年前

    尝试使用Click和Keyup,它会起作用

    // method : this will works with Click and Keyup events
    $('#start').on('click keyup',function() {
        $('#end').val($('#start').val())
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <input type="time" id="start" value="00:00">
    <input type="time" id="end" value="00:00">