Javascript jQuery使用函数返回值更改文本

Javascript jQuery使用函数返回值更改文本,javascript,php,jquery,Javascript,Php,Jquery,我想用类容器更改每个div的文本。 我有一个函数test1,它将在单击按钮后调用。 此函数应使用函数test2的返回值更改文本。但文本不会改变。alertresult显示了我期望的正确文本。我如何解决这个问题 <div class="container">Some text</div> <div class="container">Some text</div> <div class="container">Some text</

我想用类容器更改每个div的文本。 我有一个函数test1,它将在单击按钮后调用。 此函数应使用函数test2的返回值更改文本。但文本不会改变。alertresult显示了我期望的正确文本。我如何解决这个问题

<div class="container">Some text</div>
<div class="container">Some text</div>
<div class="container">Some text</div>
<div class="container">Some text</div>
<div class="container">Some text</div>


function test1() {
   $( ".container" ).each(function() { 
      $( ".container" ).text(test2('MyValue'));
   })
}

function test2(value) {  
   $.getJSON("ajax.php", {
      value: value
   }, function(result) {
        alert(result);
        return result;  
   })
}

在AJAX完成之前,您无法更改文本。。这是一个很好的解决办法

<div class="container">Some text</div>
<div class="container">Some text</div>
<div class="container">Some text</div>
<div class="container">Some text</div>
<div class="container">Some text</div>


function test1() {
   test2('myValue').then(res=>$( ".container" ).text(res))
}

function test2(value) { 
   return new Promise(done=>
   $.getJSON("ajax.php", {
      value: value
   }, function(result) {
        done(result); 
   }));
}

在我看来,您并不是在解析返回的json,.functionresult{$.container.textresult;}test2函数不返回任何内容,因为您的返回语句发生在getJSON函数的回调中。@Herohtar->这是什么意思?如何返回getJSON的值?