Javascript 从列表菜单中获取selectedIndex并将其放入函数中
这里我有一个代码Javascript 从列表菜单中获取selectedIndex并将其放入函数中,javascript,jquery,html,list,drop-down-menu,Javascript,Jquery,Html,List,Drop Down Menu,这里我有一个代码函数findPlaces,用于搜索框中的位置对象等 function findPlaces(boxes,searchIndex) { var request = { bounds: boxes[searchIndex], types: ["museum"] }; 这项工作很好,但现在 现在,我想使用列表/菜单中的值更改此类型,以便执行以下操作: //JS CODE function findPlaces(boxes,searchIndex
函数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
});
可以按如下方式检索select值:
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);
};
这里的示例:检查
类型
”值,它应该是一个int(所选选项的索引,如名称所示)。要获取所选选项的值,您必须执行以下操作:document.getElementById(“选择”)[document.getElementById(“选择”).selectedIndex].value
look here:,不再工作或此操作:是的,但我不需要更改框,我不需要此数组框“框”数组就是一个例子。同样不起作用:尝试或演示:什么时候应该调用findPlaces
?另外,您不需要使用onclick
复制零件,它的唯一目的是显示带有警报的值。我现在也尝试以下方法:$(“#选择”).change(function(){var values=$('#select option').map(function(){return this.value;}.get();var index=jQuery.inArray($)(this).val(),值);findPlaces(index);//将索引发送到函数});函数findPlaces(box,searchIndex){var request={bounds:box[searchIndex],types:[“index”]};但是不要在这里工作,但是如何在我的代码中进行调整:我尝试以下方法:$(“#选择”).change(函数(){var values=$('#选择选项').map(函数(){返回this.value;}).get();var index=jQuery.inArray($(this.val(),values);findPlaces(index);//向函数发送索引});函数findPlaces(box,searchIndex){var请求={边界:box[searchIndex],类型:[“index”]};bt不工作我不知道什么是框,但你应该定义它并调用它,替换findPlaces(索引);用findPlaces(框,索引);我只需要更改类型,然后更新函数findPlaces,就这样!–Marco Jordan刚刚编辑不不不,searchIndex有些不同,我不能用索引来更改它