从jquery方法中提取变量

exctracting a variable from a jquery method

本文关键字:提取 变量 方法 jquery      更新时间:2023-09-26

我想从mentionsInput()方法中提取变量response,并在此方法之外使用它,但是当我尝试在此变量上使用alert()时,它是空的。

jQuery(document).ready(function() {  
    var choix = $('#choixaide').val();
    var choix_sous_theme1 = $('#choix_sous_theme1aide').val();
    $('textarea.mention1').mentionsInput('val', function(text) {                
        var response = text;
    });
    alert(response);
});

谢谢你的帮助。

正如您现在所拥有的,response仅在mentionsInput方法的范围内可用,而不能在它之外。

另外,在运行代码时,我看到以下错误:

Uncaught TypeError: $(…)。

你确定你已经正确加载了jquery。提及输入UI组件?如果您也遇到这个问题,您需要先解决这个错误。

然后,您需要在mentionsInput方法之前和之外声明变量response,然后在mentionsInput中设置它。然后,response的值设置应该在与alert调用相同的作用域中可用。

我想这应该能奏效:

jQuery(document).ready(function() {
          var choix = $('#choixaide').val();
          var choix_sous_theme1 = $('#choix_sous_theme1aide').val();
          var response = $('textarea.mention1').val();
          alert(response);
        });