[英]AJAX - Button Submit to update a form
I'm attempting to learn to make use of AJAX. 我正在尝试学习使用AJAX。 I removed most of the complexity of the program to just isolate the problem I'm having.
我消除了程序的大多数复杂性,只是为了解决我遇到的问题。 So I have a text area and beneath that a div that has "STATUS" printing out.
因此,我有一个文本区域,在该区域的下面有一个div,上面打印有“状态”。 On button submit using AJAX I want to change the word "STATUS" to the value of my variable, status, which in this case should be "SUCCESS".
在使用AJAX提交按钮时,我想将单词“ STATUS”更改为变量status的值,在这种情况下应为“ SUCCESS”。
What happens instead when I click is it prints out the word STATUS. 相反,当我单击时会发生什么,它会打印出状态字。 It appears like nothing is happening when I click my submit button.
单击提交按钮时似乎没有任何反应。 Any ideas what I am doing wrong?
有什么想法我做错了吗?
$(document).ready(
function () {
$('#sub').live('click',
function () {
url = 'http://whatever.php'
success = "Success!"
$.ajax({
url: url,
type: 'POST',
data: null
dataType: 'xml',
async: false,
success: function (data, statusText, reqObj) {
status = $(data).find('status').text()
if (status == 'SUCCESS') {
$('#succ').html(status)
} //if( status == 'SUCCESS' ) {
else {
msg = $(data).find('msg').text()
alert('NOT ADDED: ' + msg)
return
} // else
} //function()
}) //$.ajax( {
} /* function */ ) //live(
} //function()
) //$(document).ready
HTML: HTML:
<div id="buttonArea">
<textarea name="txtarea" cols=80 rows=30>>THIS TEXT BOX
IS USED FOR THINGS I WILL WORK ON LATER
</textarea><br/>
<input type=submit value='Submit Textbox Info!' id='sub'>
</div>
<div class="float-left" id='succ'>STATUS</div>
I think your find is empty. 我认为您的发现是空的。 Try to replace with
尝试替换为
status = $(data).text()
I'm not sure you use .find() the right way. 我不确定您以正确的方式使用.find()。 it's used on html elements and expects to be passed a jquery selector or element or jquery object.
它用于html元素,并希望传递给jquery选择器,元素或jquery对象。
here's more details http://api.jquery.com/find/ 这是更多详细信息http://api.jquery.com/find/
what you want is to get your response text, so assign your ajax call to a variable: 您想要的是获取响应文本,因此将ajax调用分配给变量:
xmlhttp = $.ajax({
then on success use your response text: 然后在成功时使用您的回复文本:
status = xmlhttp.responseText;
Your code works fine for me, on the proviso that: 您的代码对我来说很好,但前提是:
(i) you add the missing comma from the line: (i)您添加以下行中缺少的逗号:
data: null, 数据:空,
(ii) the content-type returned by your php handler is text/xml (ii)您的php处理程序返回的内容类型为text / xml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.