javascript - 时间输入向上箭头上的keyup

标签 javascript jquery

我希望#End 的值始终等于#Start 的值,即使有人只是在#Start 上进行键盘操作而没有释放鼠标也是如此

// 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

这怎么可能?

最佳答案

使用 input 事件或绑定(bind)所有你想让它监听的事件(等同于 input-event 但我认为它更跨浏览器可靠):

//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">

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

关于javascript - 时间输入向上箭头上的keyup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48338592/

相关文章:

Javascript:在用户停止输入时进行处理

javascript - 将根渲染为 null(在无 DOM 范围内渲染)

javascript - 使用 Raphael 2.1 选择 .print 中的单个字母

每个父元素上每个第一个元素的 jQuery 选择器

javascript - 将 html 放入 iframe(使用 javascript)

php - AJAX 调用的自定义 PHP 错误

jquery - 如何构造一个 URL 来打开带有 jquery lightbox 插件 colorbox 的页面?

javascript - 如何在 useEffect 中使用 useState,访问组件挂载时更新的 useState 值?

php - 从外部网站获取内容

javascript - 单击按钮时验证表单