我有一个input
<label id="email2_error">Email is not in the correct format</label>
<input type="text" style="width:500px" id="emailTags" placeholder="add email" />
按键后我调用方法addTag
<script type="text/javascript">
$(document).ready(function () {
$("select#emailTags").select2({
placeholder: "JohnDoe@example.com",
allowClear: true
});
jQuery("#emailTags").keypress(function (event) {
//See if the key pressed is 'space'
if (event.which == 32) {
addTag();
}
//See if the key pressed is 'enter'
if (event.which == 13) {
addTag();
}
//See if the key pressed is 'tab'
if (event.which == 8) {
addTag();
}
});
});
function addTag() {
var email = $('#emailTags').val()
if (!validateEmail(email)) {
$("label#email2_error").show();
return false;
} else {
$("label#email2_error").hide();
//Adding the tag
$('#emailTags').select2('val', $('#emailTags').val());
}
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
</script>
标签未添加,有些提案?
答案 0 :(得分:0)
这是一个只需要更改脚本的解决方案
if (self.isOpen()) {
if (key === KEYS.ENTER) {
self.trigger('results:select');
evt.preventDefault();
} else if ((key === KEYS.SPACE && evt.ctrlKey)) {
self.trigger('results:toggle');
evt.preventDefault();
} else if (key === KEYS.UP) {
self.trigger('results:previous');
evt.preventDefault();
} else if (key === KEYS.DOWN) {
self.trigger('results:next');
evt.preventDefault();
} else if (key === KEYS.ESC || key === KEYS.TAB) {
self.close();
evt.preventDefault();
}
} else {
if (key === KEYS.ENTER || key === KEYS.SPACE ||
((key === KEYS.DOWN || key === KEYS.UP) && evt.altKey)) {
self.open();
evt.preventDefault();
}
}
替换为
if (self.isOpen()) {
if (key === KEYS.ENTER || key === KEYS.TAB) {
self.trigger('results:select');
evt.preventDefault();
} else if ((key === KEYS.SPACE && evt.ctrlKey)) {
self.trigger('results:toggle');
evt.preventDefault();
} else if (key === KEYS.UP) {
self.trigger('results:previous');
evt.preventDefault();
} else if (key === KEYS.DOWN) {
self.trigger('results:next');
evt.preventDefault();
} else if (key === KEYS.ESC || key === KEYS.TAB) {
self.close();
evt.preventDefault();
}
} else {
if (key === KEYS.ENTER || key === KEYS.TAB || key === KEYS.SPACE ||
((key === KEYS.DOWN || key === KEYS.UP) && evt.altKey)) {
self.open();
evt.preventDefault();
}
}