简体   繁体   English

按钮单击传递参数以进行回调

[英]Button click pass argument to callback

I have this code , very simple , not working : 我有此代码,非常简单,无法正常工作:

$(function() {
    $(document).on('click', '#NetworkSearch', NetworkMarketSearching("NETWORK"));
    $(document).on('click', '#MarketSearch', NetworkMarketSearching("MARKET"));
    $(document).on('click', '#CableSearch', NetworkMarketSearching("CABLE"));
});

you can see - I am very simply using .on() to make NetworkMarketSearching() fire from a click, here is the function. 您可以看到-我非常简单地使用.on()通过单击使NetworkMarketSearching()触发,这是该函数。 This function works just fine on its own if called from the console. 如果从控制台调用,此函数本身就可以正常工作。

function NetworkMarketSearching(types) {
    var name, searchType;
    if (types == 'NETWORK') { name = $('#NetworkName').val(); searchType = 0; }
    else if (types == 'MARKET') { name = $('#MarketName').val(); searchType = 1; }
    else if (types == 'CABLE') {name = $('#CableName').val();searchType = 2;}
    $.ajax({
        type: 'POST',
        url: '/Talent_/Common/NetworkMarketSearch',
        dataType: 'json',
        data: { 'name': name, 'searchType': searchType },
        success: function(data) {
        }
    });
}

The error is 'undefined is not a function' it repeatedly happens when putting NetworkMarketSearching('NETWORK') in the line of .on('click', ... 错误是“未定义的不是函数”错误,将NetworkMarketSearching('NETWORK')放在.on('click', ...

try this: 尝试这个:

$(function() {
    $(document).on('click', '#NetworkSearch', function() { NetworkMarketSearching("NETWORK"); });
    $(document).on('click', '#MarketSearch', function() { NetworkMarketSearching("MARKET"); });
    $(document).on('click', '#CableSearch', function() { NetworkMarketSearching("CABLE"); });
});

The click method doessnt support the string parameter, it expects the event object parameter. click方法不支持字符串参数,它需要事件对象参数。

This NetworkMarketSearching("NETWORK") calls the function immediately and attempts to assign its return result (which is undefined) as the callback. NetworkMarketSearching("NETWORK")立即调用该函数,并尝试将其返回结果(未定义)分配为回调。

You can use the data argument to pass information to your function calls: 您可以使用data参数将信息传递给函数调用:

$(function() {
    $(document).on('click', '#NetworkSearch', { types: 'NETWORK' }, NetworkMarketSearching);
    $(document).on('click', '#MarketSearch', { types: 'MARKET' }, NetworkMarketSearching);
    $(document).on('click', '#CableSearch', { types: 'CABLE' }, NetworkMarketSearching);
});

Then the function definition would be: 然后,函数定义为:

function NetworkMarketSearching(event) {

and, within the function, the types would be referenced as 并且在函数中,类型将被引用为

event.data.types

This is the way the jQuery docs specify passing arguments to the callback, but it can be done also with an inline anonymous function. 这是jQuery docs指定将参数传递给回调的方式,但是也可以使用内联匿名函数来完成。 That is to say, like this: 也就是说,像这样:

$(function() {
    $(document).on('click', '#NetworkSearch', function () {
        NetworkMarketSearching('NETWORK');
    });
    $(document).on('click', '#MarketSearch', function () {
        NetworkMarketSearching('MARKET');
    });
    $(document).on('click', '#CableSearch', function () {
        NetworkMarketSearching('CABLE');
    });
});

Are you sure the function exists at the moment you call it? 您确定函数在调用时就存在吗?

Try go simple, use this sample of jquery site ( http://api.jquery.com/on/ ): 尝试简单一点,使用此示例jquery网站( http://api.jquery.com/on/ ):

function notify() {
  alert( "clicked" );
}
$( "button" ).on( "click", notify );

then you pass a parameter, if it works you move to your code. 然后您传递一个参数,如果可行的话,您将移至您的代码。

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

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