在jquery函数参数中使用'this'

时间:2014-03-04 13:57:05

标签: javascript jquery html5 this

我在这里的第一个问题......我已经搜索了其他Q& A,但找不到我正在寻找的重点。

我是一个jquery begginer,所以,当然,这使我更难以使解决方案适应我的问题。

所以...我有以下代码:

(function($){
$('select[data-select="cidades-distinct"]').ajaxSelect('http://www.myurl.asp', #destination);
})(jQuery);

我用它来填充ajax,根据国家的州选择,用城市名称填写表格。所以...要知道哪个是包含状态选项的选择我使用数据属性data-select。城市列表以#destination ID返回。 HTML代码是:

<select data-select="cidades-distinct" id="myid" name="myname">

完美无缺。但是......如果我想使用多个州/城市列表,我就不能使用这个功能,因为它将列表返回到相同的ID #destination。

所以,我想要做的是通过数据属性从表单传递我的id目标名称,例如data-destination =“#destination”。所以,我的HTML代码是:

<select data-select="cidades-distinct" data-destination="#cidade" id="myid" name="myname">

但我不能这样做!我尝试了下面的代码以及一些变化,但是有些不对劲!

(function($){
$('select[data-select="cidades-distinct"]').ajaxSelect('http://www.myurl.asp', $(this).attr("data-destination"));
})(jQuery);

你能帮帮我吗?

编辑:添加ajaxSelect函数 这是ajaxSelect:

jQuery.noConflict();
(function($){
$.fn.extend({
    ajaxSelect: function(url, destino){
        return this.change(function(){
            var t = $(this),
                valor = t.val();

            $.ajax({
                'url': url,
                'data': {'valor': valor, 'action':'ajaxSelect'},
                'type': 'GET',
                'success': function(response){
                    $(destino).html(response)
                    $(destino).trigger("chosen:updated");
                },
                'error': function (error){
                    console.log(error);
                    loading.hide();
                    alert('Error!');
                }
            });
        });
    }
});

})(jQuery的);

1 个答案:

答案 0 :(得分:0)

“this”关键字 在JavaScript中,与大多数面向对象的编程语言一样,这是一个特殊的关键字,在方法中用于引用调用方法的对象。使用一系列简单步骤确定其值:

  • 如果使用Function.call()或Function.apply()调用该函数,则将其设置为传递给.call()/。apply()的第一个参数。如果传递给.call()/。apply()的第一个参数为null或未定义,则它将引用全局对象(它是Web浏览器中的窗口对象)。
  • 如果正在调用的函数是使用Function.bind()创建的,那么这将是在创建函数时传递给.bind()的第一个参数。
  • 如果函数被调用为对象的方法,则将引用该对象。
  • 否则,该函数将被调用为未附加到任何对象的独立函数,这将引用全局对象。


// A function invoked using Function.call()

var myObject = {
    sayHello: function() {
        console.log( "Hi! My name is " + this.myName );
    },
    myName: "Rebecca"
};

var secondObject = {
    myName: "Colin"
};

myObject.sayHello();                    // "Hi! My name is Rebecca"
myObject.sayHello.call( secondObject ); // "Hi! My name is Colin"

// A function created using Function.bind()

var myName = "the global object";
var sayHello = function() {
    console.log( "Hi! My name is " + this.myName );
};
var myObject = {
    myName: "Rebecca"
};
var myObjectHello = sayHello.bind( myObject );

sayHello();      // "Hi! My name is the global object"
myObjectHello(); // "Hi! My name is Rebecca"

// A function being attached to an object at runtime.

var myName = "the global object";
var sayHello = function() {
    console.log( "Hi! My name is " + this.myName );
};
var myObject = {
    myName: "Rebecca"
};
var secondObject = {
    myName: "Colin"
};

myObject.sayHello = sayHello;
secondObject.sayHello = sayHello;

sayHello();              // "Hi! My name is the global object"
myObject.sayHello();     // "Hi! My name is Rebecca"
secondObject.sayHello(); // "Hi! My name is Colin"

在长命名空间深处调用函数时,通常将对实际函数的引用存储为单个较短的变量,从而减少需要键入的代码量。重要的是不要使用实例方法执行此操作,因为这会导致函数中的this值发生更改,从而导致代码操作不正确。例如:

var myNamespace = {
    myObject: {
        sayHello: function() {
            console.log( "Hi! My name is " + this.myName );
        },
        myName: "Rebecca"
    }
};

var hello = myNamespace.myObject.sayHello;

您好(); //“嗨!我的名字未定义” 但是,您可以安全地减少调用该方法的对象:

var myNamespace = {
    myObject: {
        sayHello: function() {
            console.log( "Hi! My name is " + this.myName );
        },
        myName: "Rebecca"
    }
};

var obj = myNamespace.myObject;

obj.sayHello(); // "Hi! My name is Rebecca"

source

相关问题