我正在尝试使用美国,加拿大(xxx)xxx-xxxx和其他国家/地区的格式预填充电话号码字段为+ xxxxxxx。这里的问题是当我更改国家/地区逻辑似乎不因为Jquery对象保持以前的值,所以正在工作。
$(document).ready(function($) {
$('#country').on('change', function() {
if (this.value == 'US' || this.value == 'CA') {
$('#C_BusPhone')
.keydown(function(e) {
var key = e.charCode || e.keyCode || 0;
$phone = $(this);
if (key !== 8 && key !== 9) {
if ($phone.val().length === 4) {
$phone.val($phone.val() + ')');
}
if ($phone.val().length === 5) {
$phone.val($phone.val() + ' ');
}
if ($phone.val().length === 9) {
$phone.val($phone.val() + '-');
}
}
return (key == 8 ||
key == 9 ||
key == 46 ||
(key >= 48 && key <= 57) ||
(key >= 96 && key <= 105));
})
.bind('focus click', function() {
$phone = $(this);
if ($phone.val().length === 0) {
$phone.val('(');
} else {
var val = $phone.val();
$phone.val('').val(val);
}
})
.blur(function() {
$phone = $(this);
if ($phone.val() === '(') {
$phone.val('');
}
});
} else {
$('#C_BusPhone')
.keydown(function(e) {
if ($(this).val().indexOf("+") === -1) {
$(this).val("+" + $(this).val());
}
})
}
});
});
答案 0 :(得分:1)
您只是继续添加事件,它们不会被覆盖。因此,您需要手动删除该事件。由于您使用的是- (void) saveImageToFile:(NSString *) urlImg withNameNumber:(int)numberName andQuestionId:(int) questionID
{
NSString* documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString* foofile = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"gallery%d%d.jpg",numberName,questionID]];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];
if (!fileExists) {
NSData *tempImgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",@"https://testurl/",urlImg]]];
NSString *filename = [NSString stringWithFormat:@"gallery%d%d.jpg",numberName,questionID];
NSString *imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:filename];
[tempImgData writeToFile:imagePath atomically:YES];
}
}
-(UIImage*) readImageFromFile:(int)numberName andQuestionId:(int) questionID
{
NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
return [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/gallery%d%d.jpg",documentsDirectoryPath,numberName,questionID]];
}
,因此bind()
。
unbind()
请注意,根据official jQuery documentation,使用$('#C_BusPhone').unbind("keydown");
和on()
的1.7 +优先于off()
和bind()
。
另一种选择是附加一个事件并在要运行的代码内部进行逻辑检查。
答案 1 :(得分:0)
js code:
$(document).ready(function($) {
$('#country').on('change', function() {
$('#C_BusPhone').val("");
alert($("#country").val());
});
$('#C_BusPhone').keydown(function(e) {
var key = e.charCode || e.keyCode || 0;
var phone = $('#C_BusPhone');
if ($("#country").val() == 'US' || $("#country").val() == 'CA') {
console.log($("#country").val());
if (key !== 8 && key !== 9) {
if (phone.val().length === 4) {
phone.val(phone.val() + ')');
}
if (phone.val().length === 5) {
phone.val(phone.val() + ' ');
}
if (phone.val().length === 9) {
phone.val(phone.val() + '-');
}
}
if (phone.val().length === 0) {
phone.val('(');
}
else {
var val = phone.val();
phone.val('').val(val);
}
}
else {
if (phone.val().indexOf("+") === -1) {
phone.val("+" + phone.val());
}
}
});
});