2010-08-09 61 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