2010-08-12 124 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