我需要讓用戶點擊一個鏈接來顯示一個有窗體的燈箱,提交表單並將用戶重定向到一個新頁面。爲什麼在調用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