给出以下代码:

$(".force-selection").blur(function() {
        var value = $('matched-item').val();
        //check if the input's value matches the selected item
        if(value != $('#matched-item').data('selected-item')) {
            //they don't, the user must have typed something else
            $('#matched-item')
                .val('') //clear the input's text
                .data('selected-item', ''); //clear the selected item
        }
});


如何引用与$(“。force-selection”)jQuery选择器匹配的元素?我对匿名JS函数不是很清楚,而且我对人们如何有时这样声明它们感到困惑:

function()


有时像这样:

function(event)


有时像这样:

function(element, update, options)


和所有其他方式。

最佳答案

您可以将与jQuery事件对象一起传递的.currentTarget用作第一个参数:

$(".force-selection").blur(function(e) { //<-- define e as parameter for this function. it's short for "event"
        e.currentTarget; //The element a blur was triggered on
        var value = $('#matched-item').val(); //<-- add "#"



        //check if the input's value matches the selected item
        if(value != $('#matched-item').data('selected-item')) {
            //they don't, the user must have typed something else
            $('#matched-item')
                .val('') //clear the input's text
                .data('selected-item', ''); //clear the selected item
        }
});

10-05 20:45
查看更多