简体   繁体   English

在Typeahead.js的远程属性(参数)中使用选择框值

[英]Use select-box value in the remote property (parameter) of Typeahead.js

I have put together an application that makes use of Typeahead.js and a select-box containing American states. 我整理了一个使用Typeahead.js的应用程序和一个包含美国各州的选择框。

As the docs show, the library has remote property. 如文档所示,该库具有远程属性。

I want to add the value of the selected option of my select-box to remote : 我想将我的选择框的所选选项的值添加到remote

 var chooseState = function() { var jud = '', stateChoice = $('.cm-state').find('option:selected'), stateText = stateChoice.text(), jud = stateChoice.val(); console.log(jud); if (jud == '') { $('#display').hide(); } else { $('#display').fadeIn(150); } $('#choice span').text(stateText); $('#choiceVal span').text(jud); } $('.cm-state').on('change', chooseState); $('input.typeahead').typeahead({ name: 'typeahead', remote: 'search.php?key=%query&jud=' + jud, limit: 11, // My addition complete: chooseState }); 
 <script src="https://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <input class="typeahead" type="text" placeholder="Choose State"> </div> <div> <select class="cm-state"> <option value="">- Choose state -</option> <option value="AL">Alabama</option> <option value="AK">Alaska</option> <option value="CA">California</option> </select> </div> <div id="display"> <p id="choice">State name: <span></span></p> <p id="choiceVal">State code: <span></span></p> </div> 

I get a jud is not defined error message instead. 我收到一个未定义的错误消息。

Making the jud variable global with window.jud = stateChoice.val(); 使用window.jud = stateChoice.val();使jud变量成为全局变量window.jud = stateChoice.val(); does not solve the problem either. 也不能解决问题。

What am I missing? 我想念什么?

First of all, you should fetch jQuery from cdn before fetching typeahead 首先,您应该先从CDN提取jQuery ,然后再提取typeahead

Secondly, you are declaring jud variable inside the chooseState function, thus it can't be accessed from the typeahead init function on line 20 where you wrote the $('input.typeahead').typeahead({ 其次,您声明jud变量里面chooseState功能,因此不能从访问的typeahead init函数上线20 ,你写的$('input.typeahead').typeahead({

Check the snippet below. 检查下面的代码段。

 var jud = ''; // declare 'jud' variable here, so it's global in the first place; var chooseState = function() { // don't declare 'jud' here as it will become local variable for chooseSate function only; var stateChoice = $('.cm-state').find('option:selected'), stateText = stateChoice.text(); // seperate jud from var declaration now, just update the global one; jud = stateChoice.val(); console.log(jud); if (jud == '') { $('#display').hide(); } else { $('#display').fadeIn(150); } $('#choice span').text(stateText); $('#choiceVal span').text(jud); } $('.cm-state').on('change', chooseState); $('input.typeahead').typeahead({ name: 'typeahead', remote: 'search.php?key=%query&jud=' + jud, limit: 11, // My addition complete: chooseState }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- jQuery first, and then typeahead --> <script src="https://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script> <div> <input class="typeahead" type="text" placeholder="Choose State"> </div> <div> <select class="cm-state"> <option value="">- Choose state -</option> <option value="AL">Alabama</option> <option value="AK">Alaska</option> <option value="CA">California</option> </select> </div> <div id="display"> <p id="choice">State name: <span></span></p> <p id="choiceVal">State code: <span></span></p> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM