衫裤跑路 2011-05-05 11:07 采纳率: 50%
浏览 43

javascript ajax:获得价值

i have problem in getting and testing the return value of ajax. my code is, and return when alert is => Nan.

function getValue(table1, table2, id1, id2)
{
 alert("table name "+table2+" id: "+id2);

 if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
   xmlhttp=new XMLHttpRequest();
 }
 else
 {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }

 xmlhttp.onreadystatechange=function()
 {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert("ajax value: "+xmlhttp.responseText - 0);

var val =   xmlhttp.responseText - 0;

var price = document.getElementById("price");
var original= price.value;
original    =   original - 0;
var new_val =   original + val;
price.value = new_val;

document.getElementById("showprice").innerHTML=xmlhttp.responseText;
 }
}

xmlhttp.open("GET","_pages/ajax/advert_temp.php?    t1="+table1+"&t2="+table2+"&id1="+id1+"&id2="+id2+"",true);
xmlhttp.send();

}

  • 写回答

1条回答 默认 最新

  • 胖鸭 2011-05-05 11:19
    关注

    I am assuming you are talking about this alert.

    alert("ajax value: "+xmlhttp.responseText - 0);
    

    Order of operations here says

    1. Add "ajax value: " and xmlhttp.responseText
    2. Take result of 1 and subtract zero [aka "stringNumber" - 0 = NaN]

    You need to change the order of operations so it is

    alert("ajax value: " + (xmlhttp.responseText - 0) );
    

    But it really makes no difference in your alert if it is a string or a number since the alert displays strings.

    You can also use parseInt or parseFloat for converting numbers which some people might like to read instead of -0.

    评论

报告相同问题?

悬赏问题

  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员