2014-09-19 119 views
0

这种情况是,我需要通过点击一个按钮来追加一个值(一个选定的值)select2。如果我点击我的按钮,会发生什么情况,我选择的其他值是否已被清除。在按钮单击的Select2上添加额外的选定值?

只有1个值被选中,这是我按钮功能中的值。当我直接键入到select2文本框时,我可以选择多个值,但如果我单击该按钮,它的值不会添加到select2。

如何在我的点击按钮上添加一个值或将附加值推送到select2中已选择的数据?每次点击按钮时,都应该为select2选择的值添加新值。

我希望我的代码在下面,我的描述是什么即时通讯寻找有点帮助你们。谢谢。

我正在使用Northwind DataBase进行测试。 (罗伯特·金正在EMPLOYEE表)

<input type="button" onclick="Passvalue();"/> 
<input type="text" id="eq" name="eq" style="width: 200px;" /> 

<script> 
    $(function() {  
     $("#eq").select2({ 
      minimumInputLength: 3, 
      multiple: true, 
      ajax: { 
       url: '/Employee/GetAllEmployees/', 
       dataType: 'json', 
       type: "GET", 
       data: function (searhTerm) {    
        return { query: searhTerm }; 
       }, 
       results: 
        function (data) {     
         return { results: data}; 
        },        
      }, 
      initSelection: function (element, callback) { 
       var id=$(element).val(); //element value will be 'Robert'; 
       if (id!=="") { 
        $.ajax('/Employee/GetAllEmployees/', { 
         data: { 
          query: id 
         }, 
         dataType: "json", 
         type: "GET", 
        }).done(function(data) { callback(data); }); 
       }    
       }, 
      createSearchChoice: function (term) {   
       return {id: term, text: term + ' (new)', title: term };  
      }, 

      formatResult: FormatContact, 
      formatSelection: FormatContactSelection, 
      escapeMarkup: function(m) { 
       return m; 
      } 
     }); 
    }); 

    function FormatContact(contact) { 

     return contact.text + "&nbsp;(" + contact.title + ")"; 
    } 

    function FormatContactSelection(contact) { 
     return " &nbsp; &nbsp;"+ contact.text; 
    } 

    function Passvalue() { 
     var test2 = "Robert"; //just an example, value 'Robert' to be passed on select2 for query  
     $('#eq').select2("val", [test2]);  
    } 
</script> 

我的动作控制器:

public ActionResult GetAllEmployees(string query) 
      { 
       var db = new Employee().GetAllEmployees(query). 
        ToList();   
       return Json(db, JsonRequestBehavior.AllowGet); 
      } 

BL:

public IQueryable<Object> GetAllEmployees(string search) 
     { 
      var ctx = new NorthwindEntities(); 
      var dbQuery = 
      (from i in ctx.Employees 
      where i.FirstName.Contains(search) || i.LastName.Contains(search) 
      select new 
        { 
         id = i.EmployeeID, 
         text = i.FirstName + " " + i.LastName, 
         title = i.Title 
        }); 
      return dbQuery; 
     } 
+0

我对select2很不熟悉。但是你可以参考这个 http://ivaynberg.github.io/select2/select2-latest.html – 2014-09-19 12:26:03

回答

0

而不是使用 “VAL” 使用 “数据”。这样

this.$("#yourSelector").select2("data", [{ id: 1, text: "Some Text" },{ id: 2, text: "Some Other Text" }]); 

东西,所以像这样的工作,你...

var existingData = this.$("#yourSelector").select2("data"); 
existingData.push({ id: 11, text: "Some Text" }); 
this.$("#yourSelector").select2("data", existingData); 

P.S:我没有测试上面的代码。

+0

是的,这是行得通的,谢谢亚瑟! :) – alomegah 2014-10-07 10:51:47

+0

我使用下面的代码:从你的答案data.push({id:id,text:text}); $('#myselector')。select2(“data”,data,true); //刷新select2数据值 – alomegah 2014-10-07 11:09:07

相关问题