JavaScript/Ajax:填充隐藏字段

发布于 2024-09-14 06:10:09 字数 1975 浏览 11 评论 0原文

嘿,你们程序员过去对我非常有帮助,所以我想我应该在这里再次问我的问题。这可能是一个简单的修复,但我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风情万种。 2024-09-21 06:10:09

作为一个选项,您可以创建 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">

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:

<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>

Then in Form tag you can add onSubmit event handler that will return isSubmitAllowed value:

<form name="locationform" id="locationform" action="/PP?PAGE=POSTADDLOCATION" method="post" onSubmit="return isSubmitAllowed">
离不开的别离 2024-09-21 06:10:09

如果您不希望在找不到位置的情况下提交表单,最简单的方法可能是在提交之前查询服务器。这比通过其副作用检查先前可能发生或可能未发生的查询的状态更容易实现和使用。 :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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文