从列表菜单中获取选择索引并将其放入函数中
Get selectedIndex from list menu and put it in function
在这里,我有一个代码function findPlaces
,用于搜索框中放置的对象等。
function findPlaces(boxes,searchIndex) {
var request = {
bounds: boxes[searchIndex],
types: ["museum"]
};
这工作很好,但现在...
现在我想用列表/菜单中的值更改此类型,所以我这样做:
//JS CODE
function findPlaces(boxes,searchIndex) {
var request = {
bounds: boxes[searchIndex],
types: document.getElementById("select").selectedIndex
};
和 HTML 代码:
<label for="select"></label>
<select name="select" id="select">
<option>restaurant</option>
<option>bar</option>
<option>museum</option>
<option>gas_station</option>
</select>
并且此代码不会从列表/菜单中获取值并使用新值运行函数
我该如何解决这个问题?
试试这个jQuery代码,一旦你选择了元素,就会把索引发送到findPlaces,这会提醒它:
$("#select").change(function(){
var values= $('#select option').map(function(){
return this.value;
}).get();
var index=jQuery.inArray($(this).val(), values);
findPlaces(boxes, index);//send index to your function
});
在这里演示
您可以像这样检索选择值:
var boxes = ['a', 'b', 'c'];
function findPlaces(boxes, searchIndex) {
var select = document.getElementById("select")
return {
bounds: boxes[searchIndex],
types: select.options[select.selectedIndex].value
};
}
document.getElementById("select").onclick = function() {
var r = findPlaces(boxes, 2);
alert(r.types);
};
此处的示例:http://jsfiddle.net/zaCZw/
相关文章:
- 是否有任何Javascript函数从指定的索引中进行正则表达式匹配
- 使用函数索引到数组中
- D3选择html——传递函数时,索引从1开始,而不是从0开始
- 索引的从索引函数
- 给定一个带有数字的数组,我如何编写一个递归函数,当 2 个元素加起来为一个目标时,它会在数组中查找索引
- JavaScript 自包含函数数组访问自我索引
- 如何在codeigniter中的控制器索引中调用javascript函数
- 如何避免在将索引作为函数的参数发送时引用for循环中的最后一个索引
- 如何在创建编辑器函数中获取行索引
- 量角器 - 错误:第二次使用相同的函数时索引越界异常
- 我应该使用哪个索引函数
- 函数的索引满足 php
- 数组推送函数在数组中再添加一个空索引
- 如果我在 Filter() 函数中使用索引,我会得到一个未定义的引用错误吗?
- 参数“索引控制器”不是一个函数,未定义
- 返回错误索引值的函数
- 获取 array.filter 回调函数中的索引
- 动态递增 Javascript 函数参数名称的索引
- 拼接函数将相同的元素插入每个索引
- 如何将函数索引和函数数据统一为一个新函数