如何从onchange事件外部获取jQuery UI自动完成值?

时间:2018-06-28 19:08:59

标签: javascript jquery jquery-ui select autocomplete

我正在使用jQuery UI 1.12。我有一个输入字段,可以使用代码将其转换为自动完成选择

$("#myFilter").autocomplete({source: myItems});

然后我使用

在初始化时自动选择一个项目
$("#myFilter").autocomplete("search", defaultValue);

我的问题是,如何获取所选商品的所选ID?如果我参加的是“ onchange”活动,我可以做

$( "#myFilter" ).on( "autocompleteselect", function( event, ui ) {

    var id = ui.item.value;

但是,我没有参加onchange事件,因此我不确定如何提取选定的值。

2 个答案:

答案 0 :(得分:1)

窗口小部件工厂将内部数据存储在名为Option Explicit Sub ConvertDates() 'converts dates that have been mismatched MDY / DMY 'Assumes dates are all in selected column ' Only need to select a single cell in the column ' will place results in a column next to original data ' If adjacent column is not blank, a column will be inserted 'Figures out the original format by analyzing a "text" date 'Time components are converted directly. This might be OK unless ' in a non standard format such as 1400Z Dim R As Range, C As Range Dim sDelim As String Dim FileDateFormat As String * 3 Dim I As Long, J As Long, V As Variant Dim vDateParts As Variant Dim YR As Long, MN As Long, DY As Long Dim TM As Double Dim vRes As Variant 'to hold the results of conversion Set R = Selection 'Test that selected cell contains a date If Not IsDate(R(1)) Then MsgBox "Select a cell containing a date" Exit Sub End If Set R = Intersect(R.EntireColumn, ActiveSheet.UsedRange) ReDim vRes(1 To R.Rows.Count, 1 To 1) 'Find a "text date" cell to analyze For Each C In R With C If IsDate(.Value) And Not IsNumeric(.Value2) Then 'find delimiter For I = 1 To Len(.Text) If Not Mid(.Text, I, 1) Like "#" Then sDelim = Mid(.Text, I, 1) Exit For End If Next I 'split off any times V = Split(.Text & " 00:00") vDateParts = Split(V(0), sDelim) If vDateParts(0) > 12 Then FileDateFormat = "DMY" Exit For ElseIf vDateParts(1) > 12 Then FileDateFormat = "MDY" Exit For Else MsgBox "cannot analyze data" Exit Sub End If End If End With Next C If sDelim = "" Then MsgBox "cannot find problem" Exit Sub End If 'Check that analyzed date format different from Windows Regional Settings Select Case Application.International(xlDateOrder) Case 0 'MDY If FileDateFormat = "MDY" Then MsgBox "File Date Format and Windows Regional Settings match" & vbLf _ & "Look for problem elsewhere" Exit Sub End If Case 1 'DMY If FileDateFormat = "DMY" Then MsgBox "File Date Format and Windows Regional Settings match" & vbLf _ & "Look for problem elsewhere" Exit Sub End If End Select 'Process dates 'Could shorten this segment but probably more understandable this way J = 0 Select Case FileDateFormat Case "DMY" For Each C In R With C If IsDate(.Value) And IsNumeric(.Value2) Then 'Reverse the day and the month YR = Year(.Value2) MN = Day(.Value2) DY = Month(.Value2) TM = .Value2 - Int(.Value2) ElseIf IsDate(.Value) And Not IsNumeric(.Value2) Then V = Split(.Text & " 00:00") 'remove the time vDateParts = Split(V(0), sDelim) YR = vDateParts(2) MN = vDateParts(1) DY = vDateParts(0) TM = TimeValue(V(1)) Else YR = 0 End If J = J + 1 If YR = 0 Then vRes(J, 1) = C.Value Else vRes(J, 1) = DateSerial(YR, MN, DY) + TM End If End With Next C Case "MDY" For Each C In R With C If IsDate(.Value) And IsNumeric(.Value2) Then 'Reverse the day and the month YR = Year(.Value2) MN = Day(.Value2) DY = Month(.Value2) TM = .Value2 - Int(.Value2) ElseIf IsDate(.Value) And Not IsNumeric(.Value2) Then V = Split(.Text & " 00:00") 'remove the time vDateParts = Split(V(0), sDelim) YR = vDateParts(2) MN = vDateParts(0) DY = vDateParts(1) TM = TimeValue(V(1)) Else YR = 0 End If J = J + 1 If YR = 0 Then vRes(J, 1) = C.Value Else vRes(J, 1) = DateSerial(YR, MN, DY) + TM End If End With Next C End Select With R.Offset(0, 1).EntireColumn Set C = .Find(what:="*", LookIn:=xlFormulas) If Not C Is Nothing Then .EntireColumn.Insert End With R.Offset(0, 1).Value = vRes End Sub 的jquery数据属性中。对于自动完成窗口小部件,它为"ui<Widgetname>"。所选项目存储为uiAutocomplete(默认情况下,这是一个包含selectedItemlabel键的普通对象,但是可以通过source和render方法更改它,以保存更多数据。需要它)。因此,您可以这样做:

value

$("#myFilter").data("uiAutocomplete").selectedItem.label

这是一个基于用法文档的jsfiddle示例:http://jsfiddle.net/xt6482wm/3/

答案 1 :(得分:0)

可以通过多种方法访问该值。

方法1 :由于自动完成功能是在input元素上构造的,因此可以直接从该元素的jQuery对象获取该值。

方法2 :jQuery构造返回一个数组,该数组的第一个值指向元素的DOM。因此,可以使用value从DOM访问。

方法3 :jQuery将其custom data附加在元素的包装中。所有选项,默认值和方法均在对象中定义,并且它们都分配给了ui-autocomplete / uiAutocomplete键。两者均可用于访问data。由于虚线属性在内部转换为驼峰式。 MDN Docs for reference

var options = ["Tamil","English","Spanish","Italian","French"];

$("#lang").autocomplete({source:options});

function getValue()
{
    console.log("Using method 1 : "+$("#lang").val());
    console.log("Using method 2 : "+$("#lang")[0].value);
    console.log("Using method 3 : "+$("#lang").data("ui-autocomplete").selectedItem.value);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

<input id="lang">
<button onclick="getValue()">Get Selected Value</button>