admin管理员组文章数量:1421689
I've tried both keyup and change functions and here's why they don't do the trick. The user enters a voucher code and I want to send and ajax request which checks if the voucher exists in the database. I've tried:
$('#discount-voucher').keyup(function(event) {
// Get the value
let value = $(this).val()
// Check if exists
$.ajax({
url: '{{ route('get-voucher') }}',
type: 'POST',
data: {_token: '{{ csrf_token() }}', code: value}
})
.done(function(data) {
if (data == 'error') {
$('span#voucher-error').text('Неавлиден ваучер')
}
})
.fail(function() {
console.log("error")
})
.always(function() {
console.log("plete")
})
})
But those vouchers will be sent by email and let's say the user copy paste them. It will not work. And with the change function the user has to click outside of the input or to press enter for the function to take effect and I don't want that neither I want a tip/hint which says "click outside the input ot press enter to check if the voucher is valid". So the things I need is something like the change function which takes effect without clicking or pressing enter
I've tried both keyup and change functions and here's why they don't do the trick. The user enters a voucher code and I want to send and ajax request which checks if the voucher exists in the database. I've tried:
$('#discount-voucher').keyup(function(event) {
// Get the value
let value = $(this).val()
// Check if exists
$.ajax({
url: '{{ route('get-voucher') }}',
type: 'POST',
data: {_token: '{{ csrf_token() }}', code: value}
})
.done(function(data) {
if (data == 'error') {
$('span#voucher-error').text('Неавлиден ваучер')
}
})
.fail(function() {
console.log("error")
})
.always(function() {
console.log("plete")
})
})
But those vouchers will be sent by email and let's say the user copy paste them. It will not work. And with the change function the user has to click outside of the input or to press enter for the function to take effect and I don't want that neither I want a tip/hint which says "click outside the input ot press enter to check if the voucher is valid". So the things I need is something like the change function which takes effect without clicking or pressing enter
Share Improve this question edited Nov 6, 2017 at 13:14 Hassan Raza 11612 bronze badges asked Nov 6, 2017 at 12:54 Angel MiladinovAngel Miladinov 1,6554 gold badges25 silver badges46 bronze badges 3-
Why won't
keyup
/keydown
work? – freginold Commented Nov 6, 2017 at 12:59 - Because there are other ways to input values beside the keyboard. he says that in his question. – Mark Baijens Commented Nov 6, 2017 at 13:01
- @MarkBaijens Ah, got it, thanks. Thought he meant copying/pasting from the form at first. – freginold Commented Nov 6, 2017 at 13:03
1 Answer
Reset to default 8There is a paste event, you can set multiple eventlisteners like following:
$('#discount-voucher').on('keyup paste change', function () {
//Do something . . . .
});
also you may need to throttle the ajax request so it doesn't get called too often by typing.
本文标签: javascriptOn key up or change eventfire Ajax to send data in jQueryStack Overflow
版权声明:本文标题:javascript - On key up or change event, fire Ajax to send data in jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745350572a2654741.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论