0

我想用自动建议制作一个多标签并用逗号分隔。我有一个问题。每次我从建议结果中单击。它总是被新值替换。

这是我的代码:

HTML:

<input type="text" id="ftags" autocomplete="off" value="" name="tags" onkeyup="lookup(this.value);">
<div id="suggestions" style="top: 26px; display: block;">
    <p id="searchresults">
        <span class="category">AirCraft</span>
            <a href="#" class="searchlink" id="data_1"><span class="searchheading" data="Ace Aviation" id="span_1">Ace Aviation</span></a>
            <a href="#" class="searchlink" id="data_2"><span class="searchheading" data=" Ackland" id="span_2"> Ackland</span></a>
            <a href="#" class="searchlink" id="data_3"><span class="searchheading" data="Adam Aircraft" id="span_3">Adam Aircraft</span></a><span class="seperator">
            <a href="" title="Sitemap">Nothing interesting here? Try the sitemap.</a>
        </span>
        <br class="break">
    </p>
</div>

jQuery

function lookup(inputString) {
        if(inputString.length == 0) {
            $('#suggestions').fadeOut(); // Hide the suggestions box
        } else {
            $.post("modul/mod_album/rpc.php", {queryString: ""+inputString+""}, function(data) { // Do an AJAX call
                $('#suggestions').fadeIn(); // Show the suggestions box
                $('#suggestions').html(data); // Fill the suggestions box

            });
        }
}

$('.searchlink').click(function(e) {

        var parent  = $(this).closest('a').attr('id').replace('data_','');

        var val_data = $("#span_"+parent).attr('data');
        var ftag = $("#ftags").val();


       $("#ftags").val(val_data).focus();     

       e.preventDefault();

})
4

2 回答 2

0

考虑一行代码! var combo = ftag + val_data ;

var val_data = $("#span_"+parent).attr('data');
var ftag = $("#ftags").val();
var combo = ftag + val_data ;
$("#ftags").val(combo).focus();
于 2013-01-30T07:14:43.077 回答
0

此指令替换内容:

  $('#suggestions').html(data);

您可能想要按照以下方式做一些事情:

 // append to current content
  $('#suggestions').html(  $('#suggestions').html()+data); 

或者,如果您需要一些 html 来分隔标签,

  var currentTags =  $('#suggestions').html();
  if (currentTags != '')
    currentTags += ',&nbsp;';
  $('#suggestions').html( currentTags+data);
于 2013-01-30T07:12:22.943 回答