JavaScript/Ajax:填充隐藏字段
嘿,你们程序员过去对我非常有帮助,所以我想我应该在这里再次问我的问题。这可能是一个简单的修复,但我对 JavaScript 和 Ajax 很陌生。
我所做的工作是Ajax,它会写入一个responseText,说明“位置未找到”,如果文本字段失去焦点时未找到该位置。我想要做的是如果存在此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>
...
提前致谢!
Hey, you programmers have been extremely helpful to me in the past, so I thought I'd ask my question here, again. It's probably a simple fix, but I'm new to JavaScript and Ajax.
What I have working is Ajax that writes to a a responseText stating, "LOCATION NOT FOUND" if it is not found when a text field loses focus. What I'd like to have done is to prevent the form from submitting if this responseText is present. I know how to prevent the form from submitting if a hidden field has the value LOCATION NOT FOUND, so I was thinking that having JavaScript populate the hidden field with the responseText might be the easiest solution? Not sure if that will work or how to go about doing it.
Here's my current 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>
Here is the applicable part of the form:
<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>
...
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
作为一个选项,您可以创建 javascript 变量
var isSubmitAllowed = true;
,在未找到位置时将此变量设置为 false,并检查此变量以阻止表单提交。类似于:
然后在
Form
标记中,您可以添加onSubmit
事件处理程序,该处理程序将返回isSubmitAllowed
值:As an option you can create javascript variable
var isSubmitAllowed = true;
, set this variable to false when location is not found and check this variable to prevent form submitting.Something like:
Then in
Form
tag you can addonSubmit
event handler that will returnisSubmitAllowed
value:如果您不希望在找不到位置的情况下提交表单,最简单的方法可能是在提交之前查询服务器。这比通过其副作用检查先前可能发生或可能未发生的查询的状态更容易实现和使用。 :D
If you don't want the form to submit if the location isn't found, the easiest way would probably be just query the server before submitting. This be a lot easier to implement and to use than checking the status of a query that may or may not have happened previously via its side effects. :D