我的页面上有两个html元素。一个是下拉列表,另一个是文本字段(作为自动完成工作)。
<select id="match_engine_brand" name="match_engine[brand]" class="hidden-field"><option value="">Select Brand</option><option value="3">addidas</option>
<option value="5">cat</option>
<option value="2">nike</option>
<option value="4">panther</option>
<option value="6">tower</option></select>
而文字字段是
<input class="string required ui-autocomplete-input" id="match_engine_shoe_model" name="match_engine[shoe_model]" placeholder="select model of shoe using autocomplete" required="required" size="50" type="text" autocomplete="off">
我的cofeescript代码在
下面 $(document).ready ->
$("#match_engine_brand").change ->
window.flag_value = $(this).val()
alert(window.flag_value) #value display in alert
$('#match_engine_shoe_model').autocomplete
source: "/user/match_shoes/shoes?id="+window.flag_value
select: (event, ui) -> $("#match_engine_shoe_model").val(ui.item.id)
在自动填充功能
中window.flag_value #give me undefined value
$('#match_engine_brand :selected').val() #give me undefined value
我如何在自动完成功能中获得下拉值。
感谢您的帮助
答案 0 :(得分:2)
您需要具有window.flag_value
的初始值。否则,如果您不更改#match_engine_branch
,则此var没有任何价值。这是undefined value
。
解决问题的方法是在
之前定义此var$(document).ready ->
window.flag_value = "something"
...
但是我觉得使用全局变量是不必要的。检查它的功能应该有效。
$('#match_engine_shoe_model').autocomplete ->
dropdown_value = $("#match_engine_brand").val() ? "something"