简体   繁体   English

使用jQuery或JavaScript更改变量的值

[英]Change value of variable with jquery or javascript

Here I have a function which return me a list of places object based on google places API So function findPlaces is: 这里我有一个函数,该函数根据谷歌places API向我返回一个places对象列表,因此函数findPlaces是:

function findPlaces(boxes,searchIndex) {
   var request = {
       bounds: boxes[searchIndex],
            types: ["restaurant"]
   };

How I want to change this variable request - types on change on list menu so this is HTML: 我要如何更改此变量请求-列表菜单上的更改类型,因此为HTML:

<label for="select"></label>
        <select name="select" id="select">
          <option>restaurant</option>
          <option>bar</option>
          <option>museum</option>
          <option>gas_station</option>
        </select>

SO I need when I change value on list/menu to change also variable request - types["newValue"] 因此,当我更改列表/菜单上的值时也需要更改变量请求-types [“ newValue”]

How I can do that? 我该怎么做?

I was try with this code but dont work: 我尝试使用此代码,但不起作用:

function findPlaces(boxes,searchIndex) {
   var request = {
       bounds: boxes[searchIndex],
            types: document.getElementById("select")[document.getElementById("select").selectedInd‌ex].value
   };

and also try with this code but again dont work: 并尝试使用此代码,但再次不起作用:

$("#select").change(function(){

        var values= $('#select option').map(function(){
                        return this.value;
                    }).get();
        var index=jQuery.inArray($(this).val(), values);

        findPlaces(boxes, index);


    });
function findPlaces(boxes,searchIndex) {
       var request = {
           bounds: boxes[searchIndex],
                types: [index]
       };

How I can do that? 我该怎么做? So first I need to on change value in HTML change the variable request - types["value"] and update the function findPlaces ??? 因此,首先我需要在HTML中的更改值上更改变量请求-types [“ value”]并更新函数findPlaces?

Is it a singleSelect or a multiSelect? 是singleSelect还是multiSelect? But anyway you could try this: 但是无论如何,您可以尝试以下操作:

function findPlaces(boxes,searchIndex) {
   var request = {
       bounds: boxes[searchIndex],
       types: [$("#select").val()]
   };
}

Just use the jQuery object to select the #select element and use it's value (the selected element) as type. 只需使用jQuery对象选择#select元素并将其值(选定元素)用作类型。

You must take care of using DOM methods inside your objects, because you have to respect the mvc model. 您必须注意在对象内部使用DOM方法,因为您必须尊重mvc模型。

I suggest that you expose the types variable as a method and in the code that you change this type you call a method passing the new type. 我建议您将类型变量公开为方法,并在更改此类型的代码中调用传递新类型的方法。

function Places(){
    var type = "";// you could set a default value

    function findPlaces(boxes,searchIndex) {
       var request = {
           bounds: boxes[searchIndex], 
           types: [type]
       };
    }

    function changeType(value){
        type = value
    }

}

So you're using js OO, and could call changeType when the selection element change. 因此,您正在使用js OO,并且可以在选择元素更改时调用changeType。

var _place = new Places();

$("#selectionElement").change(function(){
    _place.changeType($(this).value);
});

EDIT1: 编辑1:

I made a example at fiddle: http://fiddle.jshell.net/R3xb7/1/ 我在小提琴上做了一个例子: http : //fiddle.jshell.net/R3xb7/1/

EDIT2: 编辑2:

The example with your tool: http://jsbin.com/ujExolI/1/ 工具示例: http : //jsbin.com/ujExolI/1/

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

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