WordPress 中的 jQuery 自动完成和特殊字符
希望我不会错过这个问题的现有答案。我正在使用一个 WordPress 主题,该主题使用 jquery ui 自动完成功能在前端表单中提供类别选择。代码如下。问题是,如果类别名称包含 &,则无法显示该字符,而是在自动完成框中显示 &
。我可以让它正确地显示角色吗? `
jQuery(function(){
/* Auto Complete */
var availableTags = [
<?php
$terms_array = array();
$terms = get_terms( 'majors', 'hide_empty=0' );
if ($terms) foreach ($terms as $term) {
$terms_array[] = '"'.$term->name.'"';
}
echo implode(',', $terms_array);
?>
];
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
jQuery("#majors_wrap input").live( "keydown", function( event ) {
if ( (event.keyCode === jQuery.ui.keyCode.TAB || event.keyCode === jQuery.ui.keyCode.COMMA) &&
jQuery( this ).data( "autocomplete" ).menu.active ) {
event.preventDefault();
}
}).autocomplete({
minLength: 0,
source: function( request, response ) {
// delegate back to autocomplete, but extract the last term
response( jQuery.ui.autocomplete.filter(
availableTags, extractLast( request.term ) ) );
},
focus: function() {
jQuery('input.ui-autocomplete-input').val('');
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
//this.value = terms.join( ", " );
this.value = terms.join( "" );
jQuery(this).blur();
jQuery(this).focus();
return false;
}
});
});
</script>`
Hopefully im not missing an already existing answer to this question. Im working with a wordpress theme that uses jquery ui autocomplete to provide category selections in a front end form. Code is below. Problem is that if a category name has an &, it cant display the character and instead shows &
in the autocomplete box. Can i make it show the character properly?
`
jQuery(function(){
/* Auto Complete */
var availableTags = [
<?php
$terms_array = array();
$terms = get_terms( 'majors', 'hide_empty=0' );
if ($terms) foreach ($terms as $term) {
$terms_array[] = '"'.$term->name.'"';
}
echo implode(',', $terms_array);
?>
];
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
jQuery("#majors_wrap input").live( "keydown", function( event ) {
if ( (event.keyCode === jQuery.ui.keyCode.TAB || event.keyCode === jQuery.ui.keyCode.COMMA) &&
jQuery( this ).data( "autocomplete" ).menu.active ) {
event.preventDefault();
}
}).autocomplete({
minLength: 0,
source: function( request, response ) {
// delegate back to autocomplete, but extract the last term
response( jQuery.ui.autocomplete.filter(
availableTags, extractLast( request.term ) ) );
},
focus: function() {
jQuery('input.ui-autocomplete-input').val('');
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
//this.value = terms.join( ", " );
this.value = terms.join( "" );
jQuery(this).blur();
jQuery(this).focus();
return false;
}
});
});
</script>`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需在返回的数据中替换它即可:
terms.push( ui.item.value.replace("&","&" );
Just replace it in the returned data:
terms.push( ui.item.value.replace("&","&" );