我工作的是写入responseText的Ajax,如果在文本字段失去焦点时找不到它,则说明“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>
...
提前致谢!
答案 0 :(得分:1)
作为一个选项,您可以创建javascript变量var isSubmitAllowed = true;
,在找不到位置时将此变量设置为false并检查此变量以防止表单提交。
类似的东西:
<script type="text/javascript">
<!--
var isSubmitAllowed = true;
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";
isSubmitAllowed = false;
}
else
{
dv.style.color = "black";
isSubmitAllowed = true;
}
dv.innerHTML = getLocation.responseText;
}
//-->
</script>
然后在Form
标记中添加onSubmit
事件处理程序,它将返回isSubmitAllowed
值:
<form name="locationform" id="locationform" action="/PP?PAGE=POSTADDLOCATION" method="post" onSubmit="return isSubmitAllowed">
答案 1 :(得分:0)
如果您不希望在找不到位置时提交表单,最简单的方法可能是在提交之前查询服务器。这比通过其副作用检查先前可能发生或可能不发生的查询的状态更容易实现和使用。 :d