如何从jQuery函数返回值?

时间:2022-12-05 17:11:38

I am trying to return value from jQuery function. Here is my function:

我试图从jQuery函数返回值。这是我的功能:

 $('#output').keypress(function (event){ 
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if(keycode == '13'){
        var read_lines = $('#output').val().split("\n");

        return  read_lines[read_lines.length-1];

    } 
    });

and I want to save the value it return in another variable

我想保存它在另一个变量中返回的值

var newRead = functionName(event);
alert (newRead);

How can I do this because I am not able to access function. Thanks in advance!

我怎么能这样做因为我无法访问功能。提前致谢!

2 个解决方案

#1


In event handlers, you should call other functions instead of returning a value. function(){} in your event handler is a callback function.

在事件处理程序中,您应该调用其他函数而不是返回值。事件处理程序中的function(){}是一个回调函数。

In your case, you can directly print them in the keypress event:

在您的情况下,您可以直接在按键事件中打印它们:

$('#output').keypress(function (event){ 
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if(keycode == '13'){
        var read_lines = $('#output').val().split("\n");

        alert(read_lines[read_lines.length-1]);
    } 
});

Or you can do this:

或者你可以这样做:

$('#output').keypress(function (event){ 
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if(keycode == '13'){
        var read_lines = $('#output').val().split("\n");

        MyFunction(read_lines[read_lines.length-1]);
    } 
});

function MyFunction(result){
      alert(result);
}

#2


In order to access your read value, you need function similar as below.

要访问您的读取值,您需要类似下面的功能。

functionName(newRead){
   alert(newRead);
}
$('#output').keypress(function (event){ 
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if(keycode == '13'){
        var read_lines = $('#output').val().split("\n");
         functionName(read_lines);
        //return  read_lines[read_lines.length-1];

    } 
    });

#1


In event handlers, you should call other functions instead of returning a value. function(){} in your event handler is a callback function.

在事件处理程序中,您应该调用其他函数而不是返回值。事件处理程序中的function(){}是一个回调函数。

In your case, you can directly print them in the keypress event:

在您的情况下,您可以直接在按键事件中打印它们:

$('#output').keypress(function (event){ 
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if(keycode == '13'){
        var read_lines = $('#output').val().split("\n");

        alert(read_lines[read_lines.length-1]);
    } 
});

Or you can do this:

或者你可以这样做:

$('#output').keypress(function (event){ 
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if(keycode == '13'){
        var read_lines = $('#output').val().split("\n");

        MyFunction(read_lines[read_lines.length-1]);
    } 
});

function MyFunction(result){
      alert(result);
}

#2


In order to access your read value, you need function similar as below.

要访问您的读取值,您需要类似下面的功能。

functionName(newRead){
   alert(newRead);
}
$('#output').keypress(function (event){ 
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if(keycode == '13'){
        var read_lines = $('#output').val().split("\n");
         functionName(read_lines);
        //return  read_lines[read_lines.length-1];

    } 
    });