简体   繁体   English

如何在jquery事件处理程序中使用参数调用javascript函数?

[英]How to call javascript function with parameter in jquery event handler?

I am stuck. 我被卡住了。 Searched and tried for hours. 搜索并尝试了几个小时。

EDIT: I still can't make it work. 编辑:我仍然无法使它工作。 Okay, I'll just put the source code to make it clear what I want to accomplish. 好的,我只需要输入源代码就可以清楚地说明我想要完成的任务。

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
    <script type="text/javascript">
    var date_fmt="yyyy-mm-dd";
    var time_fmt="HH:MM";
    var date_field="#id_start_0, #id_end_0"; //id refering to html input type='text'
    var time_field="#id_start_1, #id_end_1"; //id refereing to html input type='text'


    function clearFmt(fmt_type)
    { 
        if($(this).val() == fmt_type) {
            $(this).val("");

        }
    }

    function putFmt(fmt_type)
    {
        if($(this).val() == "") {
            $(this).val(fmt_type);
        }
    }


$(document).ready(function() {

    $(date_field).attr("title",date_fmt);
    $(time_field).attr("title",time_fmt);

    $(date_field).click(function() {
        clearFmt(date_fmt);
    });   

    $(date_field).blur(function(){
        putFmt(date_fmt);
    });

    $(time_field).click(function(){
        clearFmt(time_fmt);
    });   

    $(time_field).blur(function(){
        putFmt(time_fmt);
    });

});
</script>

Help ? 救命 ?

Use the jquery bind method: 使用jquery 绑定方法:

function myfunc(param) {
    alert(param.data.key);
}

$(document).ready( function() {
    $("#foo").bind('click', { key: 'value' }, myfunc);
});

Also see my jsfiddle . 另见我的jsfiddle

=== UPDATE === ===更新===

since jquery 1.4.3 you also can use: 从jquery 1.4.3开始你也可以使用:

function myfunc(param) {
    alert(param.data.key);
}

$(document).ready( function() {
    $("#foo").click({ key: 'value' }, myfunc);
});

Also see my second jsfiddle . 另见我的第二个jsfiddle

=== UPDATE 2 === ===更新2 ===

Each function has his own this . 每个功能都有自己的this After calling clearFmt in this function this is no longer the clicked element. 打完电话后clearFmt在此功能this不再是点击的元素。 I have two similar solutions: 我有两个类似的解决方案:

In your functions add a parameter called eg element and replace $(this) with element . 在你的函数添加一个名为例如,参数element和替换$(this)element

function clearFmt(element, fmt_type) {
    if (element.val() == fmt_type) {
        element.val("");
    }
}

Calling the function you have to add the parameter $(this) . 调用函数你必须添加参数$(this)

$(date_field).click(function() {
    clearFmt($(this), date_fmt);
}); 

Also see my third jsfiddle . 另见我的第三个jsfiddle

-=- - = -

An alternative: 替代:

function clearFmt(o) {
    if ($(o.currentTarget).val() == o.data.fmt_type) {
        $(o.currentTarget).val("");
    }
}
$(date_field).click({fmt_type: date_fmt}, clearFmt); 

Also see my fourth jsfiddle . 另见我的第四个jsfiddle

The following should work as seen in this live demo : 以下应该可以在本次现场演示中看到:

function myfunc(bar) {
    alert(bar);
}

$(function() {
    $("#foo").click( function() {
        myfunc("value");
    });
});
anyFunctionName = (function()
{
    function yourFunctionName1()
    {
        //your code here 
    }

    function yourFunctionName2(parameter1, param2)
    {
        //your code here 
    }

    return
    {
        yourFunctionName1:yourFunctionName1,
        yourFunctionName2:yourFunctionName2
    }
})();

$document.ready(function()
{
    anyFunctionName.yourFunctionName1();
    anyFunctionName.yourFunctionName2();
});

Note: if you don't use 'anyFuctionName' to declare any function then no need return method. 注意:如果你不使用'anyFuctionName'来声明任何函数,那么就不需要返回方法。 just write your function simply like, yourFunctionName1(). 只需像yourFunctionName1()一样编写函数。

in $document.ready section parameter is not issue. 在$ document.ready section参数中没有问题。 you just put your function name here. 你只需将你的功能名称放在这里。 if you use 'anyFuctionName' function then you have to follow above format. 如果您使用'anyFuctionName'功能,则必须遵循以上格式。

People are making this complicated, simply call directly as you would in Javascript 人们正在使这个变得复杂,只需像在Javascript中那样直接调用

myfunc("value"); MYFUNC( “值”);

function myfunc(e) {
  alert(e.data.bar); //this is set to "value"
}

$("#foo").click({bar: "value"}, myfunc);

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

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