bootstrap下拉设置选定的值

我有一个Bootstrap下拉列表

  

我想设置所选项目

 function DataLoad() { var id = $("#TourId").val(); $("#Details").load('/umbraco/Surface/Tour/GetTourDetails?tourId=' + id); } $(document).ready(function () { $("#ulGenres li a").on("click", function () { // Get text from anchor tag var id = $(this).data('pdsa-dropdown-val'); $("#TourId").val(id); // Add text and caret to the Select button var text = $(this).text(); $("#selectButton").html(text + ' '); // Put text into hidden field from model $("#SelectedText").val(text); DataLoad(); }); var id = $("#TourId").val(); $("#ulGenres li a").data("[pdsa-dropdown-val= " + id + "]").trigger("click"); //$("#ulGenres li:first-child a").trigger("click"); }); 

这不会选择所需元素的正确元素

 $("#ulGenres li a").data("[pdsa-dropdown-val= " + id + "]") 

只需更换:

$("#ulGenres li a").data("[pdsa-dropdown-val= " + id + "]")

$("#ulGenres li a").filter("[data-pdsa-dropdown-val=" + id + "]")

工作范例:

 function DataLoad() { var id = $("#TourId").val(); $("#Details").load('/umbraco/Surface/Tour/GetTourDetails?tourId=' + id); } $(document).ready(function () { DataLoad(); $("#ulGenres li a").on("click", function () { // Get text from anchor tag var id = $(this).data('pdsa-dropdown-val'); $("#TourId").val(id); // Add text and caret to the Select button var text = $(this).text(); $("#selectButton").html(text + ' '); // Put text into hidden field from model $("#SelectedText").val(text); DataLoad(); }); var id = $("#TourId").val(); $("#ulGenres li a").filter("[data-pdsa-dropdown-val=" + id + "]").trigger("click"); //$("#ulGenres li:first-child a").trigger("click"); });