2014-08-28 22 views
0

如何在按钮单击时在文本框内回显文本?这就是我要做的:用jQuery传递一个点击按钮的值

<script type="text/javascript"> 
$(function() { 
    $('#button').click(function(){ 
     $.ajax({ 
      type: "POST", 
      url: "index.php", 
      data: {text:$('#name').val()} 
     }); 
    }); 
}); 
</script> 

<button id="button" type="button">submit</button> 


<form method="post"> 
      <input type="text" id="name" name="name" size="31" placeholder="name ..." /> 
</form> 

<?php echo $_POST['text']; ?> 
+2

在一个基金会您错误地理解了浏览器,服务器端代码和HTTP请求是如何工作的。 – amphetamachine 2014-08-28 21:50:42

+0

我知道浏览器,服务器端代码和HTTP请求是如何工作的。我想在这个例子中使用AJAX。 – 2014-08-28 21:56:30

回答

0

这需要

$(function() { 
    $('#button').click(function(){ 
    var name = $('#name').val(); 
    $.ajax({ 
     type: "POST", 
     url: "index.php", 
     data: {text:name}, 
     success:function(){ $("#result").html(name); } 
    }); 
    }); 
}); 

和u需要把这些HTML标签波纹管形式<div id="result"></div>

1

如果你想显示的是从服务器的响应,你可以这样做的:

$(function() { 
    $('#button').click(function(){ 
     var xhr = $.ajax({ 
      type: "POST", 
      url: "index.php", 
      data: {text:$('#name').val()} 
     }); 
    }); 

    xhr.done(function(response){ 
     $('body').append(response); 
    }); 
}); 

在这里,我把它附在身体,因为我不知道你要插入它的位置。


如果你想显示的是在文本输入,更换符合

$('body').append($('#name').val()); 
0
$.ajax({ 
     type: "POST", 
     url: "index.php", 
     data: "text="+$('#name').val() 
});