views:

234

answers:

0

I have a page with two inputs: users_list and users_ids. jquery.autocomplete is used to autocomplete the users_list. users_ids is hidden field. When the user is selected and added to the users_list his id is added to the users_ids. Everything works fine. But the question is how to remove the value from user_ids if the user is removed from users_list?

The script:

$(document).ready(function(){
    function formatItem(row) {
      return row[0] + " (<strong>id: " + row[1] + "</strong>)";
    }
    function formatResult(row) {
      return row[0].replace(/(<.+?>)/gi, '');
    }
    $("#users_list").autocomplete("<%= url_for(:controller => :users,
                                               :action => :autocomplete_users_list) %>",
    {
      multiple: true,
      matchContains: true,
      mustMatch: true,
      autoFill: true,
      formatItem: formatItem,
      formatResult: formatResult
    });

    $("#users_list").result(function(event, data, formatted) {
      var hidden =  $("#users_ids");
        hidden.val( (hidden.val() ? hidden.val() + ";" : hidden.val()) + data[1]);
    });
  });