如何保持标记值始终附加并且不替换为新值

时间:2013-01-30 07:07:32

标签: php javascript jquery

我想制作一个带有自动建议的多标签,并用逗号分隔。和 我有个问题。我每次点击建议结果。它总是被新的价值取代。

这是我的代码:

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();

})

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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();