为什么Onchange功能在Safari中不起作用
请告诉我,如果您更改中间的字段值,则onchange函数不起作用。如果您在末尾编写数字,则该函数有效。问题仅在Safari浏览器中。有什么问题?
jQuery.fn.extend({
phone: function() {
var maskList = [{
"code": "+7 ### ### ## ##",
"country": "RU"
},
{
"code": "+380 ## ### ## ##",
"country": "UA"
}
];
var change = function(e) {
if (e.type == 'paste') $(this).val('');
cursor_start = $(this).prop('selectionStart');
cursor_end = $(this).prop('selectionEnd');
let matrix = '###############';
if ($(this).val()[0] == '+') {
matrix = '+##############';
}
maskList.forEach(item => {
let code = item.code.replace(/[\s#]/g, ''),
phone = $(this).val().replace(/[\s#-)(]/g, '');
if (phone.includes(code)) {
matrix = item.code;
}
});
let i = 0,
val = $(this).val().replace(/\D/g, '');
value = matrix.replace(/(?!\+)./g, function(a) {
return /[#\d]/.test(a) && i < val.length ? val.charAt(i++) : i >= val.length ? '' : a;
});
if (val.replace(/\D/g, '').length > value.replace(/\D/g, '').length) {
value += val.replace(/\D/g, '').slice(value.replace(/\D/g, '').length);
}
$(this).val(value);
return this;
}
return this.each(function() {
$(this).on('load keyup paste', change);
$(this).trigger('load');
});
}
});
$(".phone").phone();
$(".phone").on("change", function() {
console.log("change");
})
$(".no_phone").on("change", function() {
console.log("change");
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="phone" value="+380000000000">
<input type="text" class="no_phone" value="123">
PS仅在Safari中开放。
这仅适用于悬挂此功能的字段。其他领域通常会实现Onchange。
Please tell me, if you change the field value in the middle, then the onchange function does not work. If you write numbers at the end, then the function works. The problem is only in the safari browser. What could be the problem?
jQuery.fn.extend({
phone: function() {
var maskList = [{
"code": "+7 ### ### ## ##",
"country": "RU"
},
{
"code": "+380 ## ### ## ##",
"country": "UA"
}
];
var change = function(e) {
if (e.type == 'paste') $(this).val('');
cursor_start = $(this).prop('selectionStart');
cursor_end = $(this).prop('selectionEnd');
let matrix = '###############';
if ($(this).val()[0] == '+') {
matrix = '+##############';
}
maskList.forEach(item => {
let code = item.code.replace(/[\s#]/g, ''),
phone = $(this).val().replace(/[\s#-)(]/g, '');
if (phone.includes(code)) {
matrix = item.code;
}
});
let i = 0,
val = $(this).val().replace(/\D/g, '');
value = matrix.replace(/(?!\+)./g, function(a) {
return /[#\d]/.test(a) && i < val.length ? val.charAt(i++) : i >= val.length ? '' : a;
});
if (val.replace(/\D/g, '').length > value.replace(/\D/g, '').length) {
value += val.replace(/\D/g, '').slice(value.replace(/\D/g, '').length);
}
$(this).val(value);
return this;
}
return this.each(function() {
$(this).on('load keyup paste', change);
$(this).trigger('load');
});
}
});
$(".phone").phone();
$(".phone").on("change", function() {
console.log("change");
})
$(".no_phone").on("change", function() {
console.log("change");
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="phone" value="+380000000000">
<input type="text" class="no_phone" value="123">
P.s Only open in safari.
This does not work only for the field on which this function is hung. Other field normally fulfills onchange.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您更改其输入的价值时,iOS往往不会更改事件。
如果将
onChange
更改为Onblur
它适用于iOS,但没有其他作用。我最终提出的修复程序是同时使用onblur
和onChange
。似乎是多余的,但具有预期的效果。IOS tends to not fire change events when you change the value of their inputs.
If you change
onchange
toonblur
it works for iOS but not others. The fix I eventually came to was to use BOTHonblur
andonchange
. Seems redundant but has the desired effect.我遇到了完全相同的问题,求解与进入项目中包含的任何文件一样容易,右键单击 ,单击获取信息 go下降的方式,并确保在共享和权限中 都允许每个人都写和阅读。刷新页面和您的功能将卷入
I had the exact same problem, the solve is just as easy as going to whatever file is included in your project, right click on the icon, click on get info go all the way down and make sure that in sharing and permission everyone is allowed to both write and read. the refresh the page and your feature will wok
您只需在iPhone中的页面加载以及其他操作系统的页面老号时设置条件即可。
You can just set a condition when page load in iphone and when page laod in other os.