助けが必要です。次のケース シナリオを機能させようとして行き詰っています: メール入力フィールドがあり、次のように入力します: foo@y - yahoo.com を提供するオートコンプリート ボックスがポップアップするはずです (たとえば)。この提案に従うと、最終的な値は次のようになります: foo@yahoo.com
私はこのコードを書きました (別の jquery UI サンプルを変更しました):
$( "#tags" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
minLength: 3,
source: function( request, response ) {
var mail_regex = /^([\w.]+)@([\w.]+)$/;
var match = mail_regex.exec(request.term);
if (match)
var matcher = new RegExp( "^" + match[2], "i" );
response( $.grep( availableTags, function( item ){
return matcher.test( item );
}) );
},
focus: function() {
// 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( ", " );
return false;
}
});
完全に動作するインタラクティブなサンプル: http://jsfiddle.net/rRF2s/3/
ただし、foo@をyahoo.comだけに置き換えます-この動作をオーバーライドする方法を一生理解できません...
Javascript/jQuery マスター - 助けてください! この目標を達成する方法は?やってみました: return match[1]+matcher.test( item ) ですが、うまくいきません。