ITPUB论坛 » WEB 2.0技术 » ajax初学者求助,谢谢


2008-4-14 17:46 zhjdenis
ajax初学者求助,谢谢

一个最简单的问题,为什么输入框内容发生变化后没有反应呢?希望大家指点,谢谢!

index.jsp是首页,已经引入了prototype.js,内容如下

<form method="post" action="leavemessage" name="form1">
    <table width="584" border="1" height="130">
<tr>
<td>&nbsp;姓名</td>
<td><input type="text" maxlength="20" size="20" name="name" id="name" onchange="search()"></td></tr>
</table>
</form>
<script type="text/javascript">
  function search()
  {
  var context=$("name").value;
  var url="index2.jsp";
  var para='context='+context;
  alert(para);
  var myajax=new Ajax.Request(
  url,
   {
  method:'get',
  parameters:para,
  onComplete:show
   }
  );
  }
  
  function show(thisone)
  {
  if(thisone.readyState==4)
  {
  alert(thisone.responseText);
  }
  else alert("no");
  }


index2.jsp内容如下
<%
String context=request.getParameter("context");
System.out.println(context);
out.clear();
out.print(context);
%>
我用的是myeclipse的ajax调试平台,不是很会用现在,但是文本框发生变化时提示我已经向index2.jsp发送消息了,只是没有反应啊,index2.jsp连再console窗口也没有输出任何内容,不知为何?
还有一般都是怎么跟踪调试AJAX程序的啊,多谢指教

2008-4-15 22:15 zhjdenis
希望高手指教,谢谢

2008-4-16 12:26 super_zzw
试试url加上你的web应用context名称,比如var url="testproject/index2.jsp";或者直接试一下全路径var url="http://localhost:8080/yourproject/index2.jsp";

2008-4-18 09:34 randoming
买本基础的书看看

2008-4-21 14:43 zhjdenis
我就是从最基础的开始看的啊,不知道找个到底是什么问题,改了路径也没有用,还望指教,谢谢!!

2008-4-21 17:07 ShadowKiller
调试Javascript程序,最常用的就是alert.
但是我比较推荐使用测试工具, IE下用VS, FF 下面可以用Firebug。

另外说下并不是文本框里值发生变化时候onchange事件就会被调用。 给你一段关于onchange事件的描述
The onchange property of an Input object specifies an event-handler function that is invoked when the user changes the value displayed by a form element. Such a change may be an edit to the text displayed in input elements of type "text", "password", and "file", or the selection or deselection of a toggle button of type "radio" or "checkbox". (Radio and checkbox elements always trigger the onclick handler and may also trigger the onchange handler.) Note that this event handler is invoked only when the user makes such a change; it is not invoked if a JavaScript program changes the value displayed by an element.

Also note that the onchange handler is not invoked every time the user enters or deletes a character in a text-entry form element. onchange is not intended for that type of character-by-character event handling; instead, onchange is invoked when the user's edit is complete. The browser assumes that the edit is complete when keyboard focus is moved from the current element to some other elementfor example, when the user clicks on the next element in the form.

2008-4-26 12:42 zhjdenis
谢谢各位指点,onchange的问题我明白,焦点移出编辑框的时候触发了js事件,也看到了发送数据,就是不知道为什么没有反馈数据呢?谢谢!

2008-4-28 09:08 iooyoo
检查下有没有到index2.jsp、响应是否返回了

2008-4-29 19:33 zhjdenis
确实没有到index2.jsp,可是不知为何,我在index.jsp中用普通的链接都可以访问到index2.jsp,为什么在js的里边不行?谢谢!

2008-4-30 00:40 justforregister
书多看看, 别急着动手

页: [1]
查看完整版本: ajax初学者求助,谢谢


Powered by ITPUB论坛