嘿,您的程序员在过去对我非常有帮助,所以我想我会在这里再次提出我的问题。这可能是一个简单的修复,但我是JavaScript和Ajax的新手。JavaScript/Ajax:填充隐藏字段
我工作的是Ajax,写入一个responseText,如果在文本字段失去焦点时找不到“LOCATION NOT FOUND”。我想要做的是阻止表单提交,如果这个responseText存在。我知道如果隐藏字段的值为LOCATION NOT FOUND,我知道如何防止表单提交,所以我在考虑让JavaScript使用responseText填充隐藏字段可能是最简单的解决方案?不知道这是否会起作用或如何去做。
这是我目前的JS:
<script type="text/javascript">
<!--
function newXMLHttpRequest()
{
var xmlreq = false;
if (window.XMLHttpRequest)
{
xmlreq = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
try
{
xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2)
{
alert("Error: Unable to create an XMLHttpRequest.");
}
}
return xmlreq;
}
function getLocation(locationrouting)
{
var getLocation= newXMLHttpRequest(); // sending request
getLocation.open("GET", "/PP?PAGE=GETLOCATIONNAME&ROUTINGNUM=" + locationrouting, false);
getLocation.send(null); // getting location
var dv = document.getElementById("location_div");
if (getlocation.responseText === 'LOCATION NOT FOUND')
{
dv.style.color = "red";
}
else
{
dv.style.color = "black";
}
dv.innerHTML = getLocation.responseText;
}
//-->
</script>
这里是形式的适用部分:提前
<form name="locationform" id="locationform" action="/PP?PAGE=POSTADDLOCATION" method="post">
<tr>
<td class="ajax_msg">
<div id="location_div">/div>
</td>
</tr>
<tr>
<td>
<div class="column1" id="locationrouting_div">
<p class="label_top">
*Routing Number</p>
<p class="field_top">
<input type="text" id="locationrouting" name="locationrouting" size="28" maxlength="9" onblur="getLocation(this.value);" /></p>
...
谢谢!
同意。但是我的雇主希望我在前端做到这一点。在后端工作的程序员是我的老板,他宁愿让我找到解决方案。 – Spockrates 2010-08-12 16:45:35
您可以使用JavaScript实现您的解决方案,还是必须在服务器上配置它? – Spockrates 2010-08-12 17:23:57
@ Spockrates它只是一个发送另一个Ajax请求并提交表单的问题,如果响应文本!==“LOCATION NOT FOUND” – 2010-08-12 21:04:51