简体   繁体   English

如何将值传递给用作参数的函数?

[英]How do i pass an value to a function that i use as parameter?

i want to pass a function as an parameter if an element was clicked. 如果要单击元素,我想将函数作为参数传递。 But in the function that i pass i want to set an attribute information from the clicked element. 但是在我通过的函数中,我想从单击的元素中设置属性信息。

What is the best way to pass the attribute value to the inner of the function? 将属性值传递到函数内部的最佳方法是什么?

My JavaScript looks like this: 我的JavaScript看起来像这样:

$('.edit-link').livequery(function(){
    $('.edit-link').click(function(){
        WMT.openOverlay($('#edit'),{
            relativeTo: $('.search'),
            width: $('.search'),
            close: $('#cancel_edit'),
            fillFields: function(){
                return $.getJSON("ajax/edit.php?id=" + $(clickedElement).data('id'));
            }
        });
    });
});

i want to send the data attribute "id" in the json. 我想在json中发送数据属性“ id”。

You need to use this . 您需要使用this It pointed to clicked element: 它指向点击的元素:

$(this).data('id')

If this didn't work try this: 如果这不起作用,请尝试以下操作:

var clickedElement = this;
WMT.openOverlay($('#edit'),{
// ... rest of your code.

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

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