我需要让用户点击一个链接来显示一个有窗体的灯箱,提交表单并将用户重定向到一个新页面。为什么在调用JavaScript函数后页面会遇到NullPointerException?
为了实现这一点,我定义了两个javascript函数,lightboxform函数来显示lightbox和子窗体函数来提交表单。问题是第二个javascript函数(子表单)将请求发送到后端,而不是重定向到索引页面,将其参数追加到原始地址。
比方说,我在下面的地址:
www.example.com/show?id=2
我点击链接以显示灯箱,提交位于灯箱的形式后,页面地址得到改变到以下地址:
www.example.com/show?p=3444&q=4555 << id parameter is replaced by p,q which are the parameters that I am trying to submit to backend
长话短说:我需要让用户点击一个链接,显示一个灯箱,用户将灯箱中的表单提交到后端并重定向到索引页面。
代码
//function to show lightbox
function lightboxform(){
document.getElementById("lightbox").style.display = "Block";
document.getElementById("cover").style.display = "Block";
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
document.getElementById("lightbox").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("get","../../lightbox/myform",false);
xmlhttp.send();
return false;
}
//function to submit the lightbox form to backend and redirect user to new address
function subform(){
p = $('#p').val();
q = $('#q').val();
document.getElementById("lightbox").style.display = "Block";
document.getElementById("cover").style.display = "Block";
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
document.getElementById("lightbox").style.display = "none";
document.getElementById("cover").style.display = "none";
window.location = ("http://localhost:8080/index.action");
}
}
xmlhttp.open("get","../../lightbox/processForm?p="+p+"&q="+q,false);
xmlhttp.send();
}
灯箱的形式
<form id="form393" onsubmit="return subform()">
<input id="p" name="p" type="text"/>
<input id="q" name="q" type="text"/>
<input type="submit" value="submit" />
</form>
向我们展示您的html。也给出确切的错误信息。 – Daedalus
另外,你的网址根本不匹配。节目从哪里来?我看到processForm和myform,但没有myproducts等。 – Daedalus
问题已更新,我正在编辑html文件以上传它。 – AlexCartio1