简体   繁体   English

如何将Ajax调用发送到INPUT值

[英]How to send Ajax call to an INPUT value

My knowledge about javascript is very low; 我对javascript的了解很少。 thus, this is a simple question. 因此,这是一个简单的问题。 I have a simple jQuery Ajax function as 我有一个简单的jQuery Ajax函数

$(document).ready(function(){
   $('#txtValue').keyup(function(event){
      if(event.which == 13){
            sendValue($(this).val());   
      }
   }); 
});

function sendValue(str){
    $.post("test.php",{ sendValue: str },
    function(data){
        $('#display').html(data.returnValue);
    }, "json");
}

This sends the json response to with id="display" 这会将json响应发送到id =“ display”

<input type="text" name="txtValue" value="" id="txtValue">
<div id="display"></div>

But instead I want to put the json response as value of another INPUT element in a different form as 但是相反,我想将json响应作为另一个INPUT元素的值以不同的形式设置为

<form method="get" action="result.php">
<input id="DISPLAY" type="text" name="something" value="" />
<input type="submit" value="Submit" />
</form>

I want to put json response to and instead of 我想将json响应放到而不是

if the form is on the same page : 如果表单在同一页面上:

$(document).ready(function(){
   $('#txtValue').keyup(function(event){
      if(event.which == 13){
         sendValue($(this).val());   
      }
    }); 
});

function sendValue(str){
   $.post("test.php",{ sendValue: str },
   function(data){
       $(':input#DISPLAY').html(data.returnValue);
   }, "json");
}

use .val() 使用.val()

$(document).ready(function(){
  $('#txtValue').keyup(function(event){
     if(event.which == 13){
        sendValue($(this).val());   
     }
  }); 
});
function sendValue(str){
  $.post("test.php",{ sendValue: str },
  function(data){
     $("#display").val(data.returnValue);           
  }, "json");
}

如果要输入返回值,则需要使用.val(data.returnValue)而不是.html(data.returnValue)

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

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