JQuery设置bootstrap选择选择器的预选值

时间:2014-08-26 16:31:55

标签: javascript jquery twitter-bootstrap bootstrap-select

我想从下面的下拉列表中选择多个值。对于这个例子,我想选择'Abbey'& '贝尔格雷夫'

<select id="dropdown" multiple>
    <option value='Belgrave'>Belgrave</option>
    <option value='Abbey'>Abbey</option>
    <option value='Castle'>Castle</option>
</select> 

我可以手动实现这一点,这样可以正常工作:

$('#dropdown').selectpicker('val', ['Abbey', 'Belgrave']);

问题是通过在字符串中传递数组它不起作用:

var wards = dsFilterOptions.Wards.split(",");
var strWard = "[";
for (i = 0; i < wards.length; i++) {
    strWard += "'" + wards[i] + "',";
}
strWard = strWard.slice(0, -1); //remove last comma
strWard += "]";
$('#dropdown').selectpicker('val', strWard);

上面没有选择修道院和修道院。贝尔格雷夫的价值观。我能做什么。

2 个答案:

答案 0 :(得分:6)

您可以使用JSON.parse将字符串恢复为数组,因此:

$('#dropdown').selectpicker('val', JSON.parse(strWard));

编辑,单引号已修复:

var wards = dsFilterOptions.Wards.split(",");
var strWard = '[';
for (i = 0; i < wards.length; i++) {
    strWard += '"' + wards[i] + '",';
}
strWard = strWard.slice(0, -1); //remove last comma
strWard += ']';
$('#dropdown').selectpicker('val', JSON.parse(strWard));

答案 1 :(得分:0)

using DesignTools.ViewModels; using Microsoft.WindowsAzure.MobileServices; using MIISiteDataModel.Common; using MIISiteDataModel.DataModels; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using System.Threading.Tasks; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; // The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236 namespace DesignTools.Controls { public sealed partial class ItemSpecLookup : UserControl { public ItemSpecLookup() { this.InitializeComponent(); this.DataContext = this; //pull in mfg table? AzureService.ManufacturerTable.PullAsync(null,AzureService.ManufacturerTable.CreateQuery()); } #region Dependencies public static readonly DependencyProperty itemGUID = DependencyProperty.Register("ItemGUID", typeof(string), typeof(ItemSpecLookup), new PropertyMetadata(null)); public string ItemGUID { get { return (string)GetValue(itemGUID); } set { SetValue(itemGUID, value); } } #endregion #region Properties private ObservableCollection<ItemSpecificationData> _itemSearchList = new ObservableCollection<ItemSpecificationData>(); public ObservableCollection<ItemSpecificationData> ItemSearchList { get { return _itemSearchList; } set { _itemSearchList = value; } } #endregion public async void ItemSearchQuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args) { if (args.ChosenSuggestion != null) { ItemSpecificationData selectedItem = args.ChosenSuggestion as ItemSpecificationData; ItemGUID = selectedItem.Id; } else { ContentDialog invalidSite = new ContentDialog() { Title = "Invalid Part Number", Content = string.Format("'{0}' was not found in the database...", args.QueryText), CloseButtonText = "Ok" }; await invalidSite.ShowAsync(); } } private async Task<string> IDtoMFG(string id) { ManufacturersData mfg = await AzureService.ManufacturerTable.LookupAsync(id); return mfg.Company; } public async void ItemSearchSearching(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args) { Debug.WriteLine("arg reason: " + args.Reason.ToString()); switch (args.Reason) { case AutoSuggestionBoxTextChangeReason.ProgrammaticChange: { break; } case AutoSuggestionBoxTextChangeReason.SuggestionChosen: { //IMobileServiceTable<ItemSpecificationData> OnlineItemList = AzureService.AzureClient.GetTable<ItemSpecificationData>(); //ASB.ItemsSource = await OnlineItemList.Where(i => i.Id == ItemGUID).ToCollectionAsync(); break; } case AutoSuggestionBoxTextChangeReason.UserInput: { IMobileServiceTable<ItemSpecificationData> OnlineItemList = AzureService.AzureClient.GetTable<ItemSpecificationData>(); if (sender.Text != string.Empty) //ItemSearchList = await OnlineItemList.Where(i => i.ItemPartNumber.Contains(sender.Text)).Take(25).OrderBy(i => i.ItemPartNumber).ToCollectionAsync(); ItemSearchList = await OnlineItemList.Select(i => new ItemSpecificationData { Id = i.Id, ItemManufacturer = i.ItemManufacturer, ItemPartNumber = i.ItemPartNumber, ItemModel = i.ItemModel, ItemDescription = i.ItemDescription, ItemCost = i.ItemCost, ItemDiscontinued = i.ItemDiscontinued }).Where(i => i.ItemPartNumber.Contains(sender.Text) || i.ItemModel.Contains(sender.Text)).Take(250).ToCollectionAsync(); else //ItemSearchList = await OnlineItemList.Take(25).OrderByDescending(i => i.UpdatedAt).ToCollectionAsync(); ItemSearchList = await OnlineItemList.Select(i => new ItemSpecificationData { ItemManufacturer = i.ItemManufacturer, ItemPartNumber = i.ItemPartNumber, ItemModel = i.ItemModel, ItemDescription = i.ItemDescription, ItemCost = i.ItemCost, ItemDiscontinued = i.ItemDiscontinued }).Take(250).OrderByDescending(i => i.UpdatedAt).ToCollectionAsync(); foreach (ItemSpecificationData item in ItemSearchList) { item.ItemManufacturer = await IDtoMFG(item.ItemManufacturer); } ASB.ItemsSource = ItemSearchList.OrderBy(i => i.ItemManufacturer); break; } } } public void ItemSearchChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args) { ItemSpecificationData test = args.SelectedItem as ItemSpecificationData; } private void rp_SizeChanged(object sender, SizeChangedEventArgs e) { RelativePanel rp = sender as RelativePanel; rp.Width = ASB.ActualWidth - 24; //change visual states } } } 是一个数组,但它只显示第一个值作为下拉选择,而不是数组的所有值。请建议

&#13;
&#13;
response.user_accounts
&#13;
&#13;
&#13;