Jquery 如何在表单字段中填充ajax POST值?
现在,我如何访问发布的“数据”并在HTML表单中填充文本字段。在ajax发布期间,我没有单击任何按钮,我希望这样做而不必重新加载整个页面。非常感谢您的帮助。请尝试以下代码示例:Jquery 如何在表单字段中填充ajax POST值?,jquery,html,ajax,forms,post,Jquery,Html,Ajax,Forms,Post,现在,我如何访问发布的“数据”并在HTML表单中填充文本字段。在ajax发布期间,我没有单击任何按钮,我希望这样做而不必重新加载整个页面。非常感谢您的帮助。请尝试以下代码示例: $(document).ready(function() { $('#content-area').mouseup(function() { var selection = getSelected(); if(selection && (selection = new String(sel
$(document).ready(function() {
$('#content-area').mouseup(function() {
var selection = getSelected();
if(selection && (selection = new String(selection).replace(/^\s+|\s+$/g,''))){
$.ajax({
type: 'POST',
url : 'check.php',
data: 'selection=' + encodeURI(selection),
});
}
});
您需要在ajax调用中定义a
success
处理程序。此处理程序将接受三个参数,第一个是从服务器接收回来的数据
$(document).ready(function () {
$('#content-area').mouseup(function () {
var selection = getSelected();
if (selection && (selection = new String(selection).replace(/^\s+|\s+$/g, ''))) {
$.ajax({
type: 'POST',
url: 'check.php',
data: 'selection=' + encodeURI(selection),
beforeSend: function () {
//do something
}
success: function (response) {
$("TEXTFIELD_ID").val(response);
}
});
}
});
请注意,
text\u input\u选择器
必须是文本输入的有效选择器写入成功回调处理程序。像
$(document).ready(function() {
$('#content-area').mouseup(function() {
var selection = getSelected();
if(selection && (selection = new String(selection).replace(/^\s+|\s+$/g,''))){
$.ajax({
type: 'POST',
url : 'check.php',
data: 'selection=' + encodeURI(selection),
success: function(data){
alert(data.d);
$('text_input_selector').val(data.d);
}
});
}
});
你说的是从服务器返回的数据吗?@Leo是的,我想从ajax帖子中访问'selection'变量,并在选中文本后填充表单文本字段。你希望得到什么样的数据
json
,xml
,等等?Leo只是一个普通的文本字符串。我提醒了'selection'变量,它工作正常。但是,我不完全知道如何从发布的值填充文本字段。谢谢
$.ajax({
type: 'POST',
url : 'check.php',
data: 'selection=' + encodeURI(selection),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
var returnedvalue = result.d;
debugger;
},