Javascript Ajax代码在第一次计算后停止
我正在编写计算费用的ajax代码。Javascript Ajax代码在第一次计算后停止,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在编写计算费用的ajax代码。 它工作正常,但如果我进入第二个操作,它就会停止 <script> var weight = document.getElementById("weight").value; var ship_type = document.getElementById("ship_type").value; var eol = document.getElementById("eol").value; function showFees(e) { e.pr
它工作正常,但如果我进入第二个操作,它就会停止
<script>
var weight = document.getElementById("weight").value;
var ship_type = document.getElementById("ship_type").value;
var eol = document.getElementById("eol").value;
function showFees(e) {
e.preventDefault();
if (weight === 0) {
document.getElementById("txtHint").innerHTML = "no thing";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "sdr.php?weight=" + weight + "&ship_type=" + ship_type + "&eol=" + eol, true);
xmlhttp.send();
}
}
</script>
var-weight=document.getElementById(“weight”).value;
var ship_type=document.getElementById(“ship_type”).value;
var eol=document.getElementById(“eol”).value;
功能展示费(e){
e、 预防默认值();
如果(权重==0){
document.getElementById(“txtHint”).innerHTML=“无事”;
返回;
}否则{
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
document.getElementById(“txtHint”).innerHTML=this.responseText;
}
};
open(“GET”、“sdr.php?weight=“+weight+”&ship_type=“+ship_type+”&eol=“+eol,true”);
xmlhttp.send();
}
}
这是因为您在函数外部获取输入值,它会第一次加载值,并且在调用showFees
函数时会一次又一次地获取相同的数据。尝试下面的代码来解决这个问题
var objWeight = document.getElementById("weight");
var objShip_type = document.getElementById("ship_type");
var objEol = document.getElementById("eol");
function showFees(e) {
var weight = objWeight.value;
var ship_type = objShip_type.value;
var eol = objEol.value;
e.preventDefault();
if (weight === 0) {
document.getElementById("txtHint").innerHTML = "no thing";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "sdr.php?weight=" + weight + "&ship_type=" + ship_type + "&eol=" + eol, true);
xmlhttp.send();
}
}
欢迎将来,您应该解决您的问题,就像您多次呼叫
showFees
,并且响应没有变化一样。这样用户在回答时不会产生任何混淆。