English 中文(简体)
单击和输入的不同行为
原标题:Different behavior of click and enter

点击提交按钮时, ajax 工作, php 页面被调用( 没有刷新), 但按下键时, 刷新页面, 脚本不工作, 只有 < code>? txtname=( INPUTED TEXT) 出现在页面 URL 末尾 。

ajax 代码 :

var time_variable;
var root2 = location.protocol +  //  + location.host +  /testing.php ;

function getXMLObject()  //XML OBJECT
{
   var xmlHttp = false;
   try {
     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
   }
   catch (e) {
     try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
     }
     catch (e2) {
       xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
     }
   }var root = location.protocol +  //  + location.host;
   if (!xmlHttp && typeof XMLHttpRequest !=  undefined ) {
     xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
   }
   return xmlHttp;  // Mandatory Statement returning the ajax object created
}

var xmlhttp = new getXMLObject();   //xmlhttp holds the ajax object

function ajaxFunction() {
  var getdate = new Date();  //Used to prevent caching during ajax call
  if(xmlhttp) {
    var txtname = document.getElementById("txtname");
    xmlhttp.open("POST",root2,true); //calling testing.php using POST method
    xmlhttp.onreadystatechange  = handleServerResponse;
    xmlhttp.setRequestHeader( Content-Type ,  application/x-www-form-urlencoded );
    xmlhttp.send("txtname=" + txtname.value); //Posting txtname to PHP File
  }
}

function handleServerResponse() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
       document.getElementById("message").innerHTML=xmlhttp.responseText; //Update the HTML Form element
     }
     else {
        alert("Error during AJAX call. Please try again");
     }
   }
}

html 部分 :

<body>
<form name="myForm">
<table>
 <tr>
  <td>Enter Name</td>
  <td><input type="text" name="txtname" id="txtname" value="<?php echo $f ?>" /></td>
 </tr>
 <tr>
  <td colspan="2"><input type="button" value="Submit" onclick="ajaxFunction();" /></td>
 </tr>
</table>
</form>
<div id="message" name="message"></div>
</body>
最佳回答

您需要将 onsumbit 属性添加到您的窗体 :

<form name="myForm" onsubmit="ajaxFunction(); return false;">

return fraud; 是指防止页面更新(即实际提交而不是ajax)。

问题回答

应用程序来提交窗体,而不是单击按钮

<form name="myForm" onsubmit="ajaxFunction();">

您的函数将以此方式在任何情形下工作, 通过输入或按键单击提交表单 。

替换

<form name="myForm">

<form name="myForm" onsubmit="ajaxFunction();return false;">

because the form will submit itself 与GET method on the page itself 与ENTER unless you define what to do on ENTER keypress.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签