2010-08-09 59 views
0

嘿,我想根據結果更改字體顏色或responseText。例如,如果responseText未找到,我希望字體顏色爲紅色。否則,它會變黑。它目前顯示正確的responseText;我只是想在必要時改變顏色。這是我目前的Ajax:Ajax或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(location) 
    { 
    var getLocation= newXMLHttpRequest(); // sending request 
    getLocation.open("GET", "/PP?PAGE=GETLOCATIONNAME&ROUTINGNUM=" + location, false); 
    getLocation.send(null); // getting location 
    document.getElementById("location_div").innerHTML = getLocation.responseText; 
    } 

這個responseText將在HTML表格內:

    <tr> 
         <td class="ajax_msg"> 
          <div id="location_div"></div>       
         </td> 
         <td>&nbsp;</td> 
        </tr> 
        <tr> 
         <td> 
          <div class="column1"> 
           <p class="label_top"> 
*Routing Number</p> 
           <p class="field_top"><input type="text" id="location" name="location" size="28" maxlength="9" onblur="getLocation(this.value);" /></p> 

任何建議都歡迎。提前致謝!

回答

0

修改你的代碼是這樣的:

function getLocation(location) 
    { 
    var getLocation= newXMLHttpRequest(); // sending request 
    getLocation.open("GET", "/PP?PAGE=GETLOCATIONNAME&ROUTINGNUM=" + location, false); 
    getLocation.send(null); // getting location 

    var dv = document.getElementById("location_div"); 

    if (getLocation.responseText === 'NOT FOUND'){ 
     dv.style.color = "red"; 
    } 

    dv.innerHTML = getLocation.responseText; 
    } 

你基本上檢查響應文本條件與:

if (getLocation.responseText === 'NOT FOUND'){ 

而且使用style這樣的改變它的顏色:

dv.style.color = "red"; 

請注意,dv變量表示您將顯示respo的div這一點我們已經在這一行設置網元文本:

var dv = document.getElementById("location_div"); 

更新:

與else條件嘗試,因爲你可能在默認情況下有紅色:

if (getLocation.responseText === 'NOT FOUND'){ 
    dv.style.color = "red"; 
} 
else { 
    dv.style.color = "black"; 
} 
+0

感謝您的答覆。不知道爲什麼,但它不起作用。如果我註釋掉在檢查這樣的響應: \t \t功能的getLocation(位置) \t \t { \t \t變種的getLocation = newXMLHttpRequest(); getLocation.open(「GET」,「/ PP?PAGE = GETLOCATIONNAME&ROUTINGNUM =」+ location,false); \t \t getLocation.send(null); \t \t \t \t \t var dv = document.getElementById(「location_div」); \t \t \t \t \t //如果(getLocation.responseText == '未找到') \t \t // { \t \t \t dv.style.color = 「紅」; \t \t //} \t \t dv.innerHTML = getLocation.responseText; \t \t} 那麼字體是紅色的。到現在爲止還挺好。 – Spockrates 2010-08-09 19:46:50

+0

但是如果我不評論它是這樣的: \t \t功能的getLocation(位置) \t \t { \t \t VAR的getLocation = newXMLHttpRequest(); getLocation.open(「GET」,「/ PP?PAGE = GETLOCATIONNAME&ROUTINGNUM =」+ location,false); \t \t getLocation.send(null); \t \t \t \t \t var dv = document.getElementById(「location_div」); \t \t \t \t \t如果(getLocation.responseText == '未找到') \t \t { \t \t \t dv.style.color = 「紅」; \t \t} \t \t dv.innerHTML = getLocation.responseText; \t \t} 然後字體保持黑色。任何想法,爲什麼它不適用風格? – Spockrates 2010-08-09 19:48:21

+0

請參閱我的更新。 – Sarfraz 2010-08-09 19:52:43