How to call javascript function with parameter in

2020-08-26 10:59发布

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 ?

5条回答
爷、活的狠高调
2楼-- · 2020-08-26 11:26

Use the jquery bind method:

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

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

Also see my jsfiddle.

=== UPDATE ===

since jquery 1.4.3 you also can use:

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

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

Also see my second jsfiddle.

=== UPDATE 2 ===

Each function has his own this. After calling clearFmt in this function this is no longer the clicked element. I have two similar solutions:

In your functions add a parameter called e.g. element and replace $(this) with element.

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

Calling the function you have to add the parameter $(this).

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

Also see my third 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.

查看更多
倾城 Initia
3楼-- · 2020-08-26 11:29
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. just write your function simply like, yourFunctionName1().

in $document.ready section parameter is not issue. you just put your function name here. if you use 'anyFuctionName' function then you have to follow above format.

查看更多
地球回转人心会变
4楼-- · 2020-08-26 11:30
function myfunc(e) {
  alert(e.data.bar); //this is set to "value"
}

$("#foo").click({bar: "value"}, myfunc);
查看更多
地球回转人心会变
5楼-- · 2020-08-26 11:35

The following should work as seen in this live demo:

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

$(function() {
    $("#foo").click( function() {
        myfunc("value");
    });
});
查看更多
狗以群分
6楼-- · 2020-08-26 11:36

People are making this complicated, simply call directly as you would in Javascript

myfunc("value");

查看更多
登录 后发表回答