问题描述
下面是我在主页面上的功能。这个代码在IE中工作正常,请检查我在哪里做错了
below is my function on main page.this code works fine in IE, please check where i am doing mistake
我只想将值传递给ajax页面你可以在上面看到我的链接。
i just want to pass values to ajax page as you can see my link above.
function showUser2(str2)
{
if (str2=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
var drdownValue = document.getElementsByName('shorting').item(0).value;
var buttonValue1 = document.getElementsByName('buttonpassvalue1').item(0).value;
var value = parseInt(document.getElementById('count').value, 10);
value = isNaN(value) ? 0 : value;
value++;
document.getElementById('count').value = value;
var val123 = document.getElementById('count').value.trim();
var gotnumber = document.getElementById('getpagevalue').value.trim();
if(val123 > gotnumber){
buttonpassvalue1.setAttribute("disabled","disabled");
buttonpassvalue.removeAttribute("disabled");
} else{
buttonpassvalue.removeAttribute("disabled");
buttonpassvalue1.removeAttribute("disabled");
}
alert(drdownValue);// to check click and value
alert(buttonValue1);// to check click and value
xmlhttp.open("GET", "productlistajax.php?q=" + drdownValue + "&version1=" + buttonValue1, true);
xmlhttp.send();
}
按钮html代码
<button class="button" type="button" name="buttonpassvalue1" value="1" onclick="showUser2(this.value)">Next >> </button>
此代码在IE中运行正常,请检查我在哪里做错了
this code works fine in IE, please check where i am doing mistake
我只想将值传递到ajax页面,因为你可以看到我上面的链接。
i just want to pass values to ajax page as you can see my link above.
谢谢
推荐答案
我建议使用jQuery或其他框架,它将始终了解所有特定于浏览器的东西。
I recommend to use jQuery or other framework, that will be always aware of all the browsers specific stuff.
如果您更喜欢使用本机代码,则需要一些额外的代码。首先,不要仅仅依赖于 ActiveXObject(Microsoft.XMLHTTP);
和 XMLHttpRequest
,它们可能永远不可用。而不是那样,使用:
If you prefer to use native code, you will need some extra code. First, dont rely just in ActiveXObject("Microsoft.XMLHTTP");
and XMLHttpRequest
that probably wont be always available. instead of that, use:
function GetXmlHttpObject(){
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
var xmlhttp=GetXmlHttpObject();
然后你定义你的回调:
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
并发送它,但我会包含一个随机变量在网址中,以确保浏览器不缓存任何内容
and send it, but i would include a random variable in the url, to ensure that the browser is not caching anything
xmlHttp.open("GET",url+"&sid="+Math.random(),true);
xmlHttp.send(null);
无论如何,可能你的问题与ajax本身无关,并且可能在你的代码中还有其他东西会导致一切崩溃。
Anyways, probably your problem has nothing to do with the ajax itself, and probably there is something else in your code causing everything to crash.
例如,看一下chrome控制台,我确定你会有一些有用的信息错误日志。如果您不知道如何,请在这里
For example, take a look at the chrome console, and im sure you will have some useful informtion in the error log. if you dont know how, here you have a guide
这篇关于Ajax在Chrome,Firefox和&歌剧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!