2010-08-12 127 views
0

嘿,您的程序员在过去对我非常有帮助,所以我想我会在这里再次提出我的问题。这可能是一个简单的修复,但我是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> 

    ... 

谢谢!

回答

0

如果您不希望表单提交,如果找不到位置,最简单的方法可能只是在提交之前查询服务器。这比通过其副作用检查以前可能发生或可能不发生的查询的状态要容易实施和使用。 :D

+0

同意。但是我的雇主希望我在前端做到这一点。在后端工作的程序员是我的老板,他宁愿让我找到解决方案。 – Spockrates 2010-08-12 16:45:35

+0

您可以使用JavaScript实现您的解决方案,还是必须在服务器上配置它? – Spockrates 2010-08-12 17:23:57

+0

@ Spockrates它只是一个发送另一个Ajax请求并提交表单的问题,如果响应文本!==“LOCATION NOT FOUND” – 2010-08-12 21:04:51

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"> 
+0

听起来有希望,但不太清楚如何去做。你有一个例子吗? – Spockrates 2010-08-12 17:15:51

+0

你的意思是这样吗?函数getLocation(locationrouting){ar getLocation = newXMLHttpRequest(); getLocation.open(“GET”,“/ PP?PAGE = GETLOCATIONNAME&ROUTINGNUM =”+ locationrouting,false); getLocation.send(null); var dv = document.getElementById(“location_div”); if(getlocation.responseText ==='LOCATION NOT FOUND') { dv.style.color =“red”; \t var isSubmitAlowed = false; } else { dv.style.color =“black”; \t var isSubmitAlowed = true; } dv.innerHTML = getLocation.responseText; } – Spockrates 2010-08-12 17:36:15

+0

我已更新我的帖子,希望这会有所帮助 – 2010-08-12 17:54:39