簡體   English   中英

使用jsp調用javascript函數時出現問題

[英]Problem while calling a javascript function using jsp

<form name='frm' action="Readcsvv" enctype="multipart/form-data" method="POST" > 
<input type="file" name="file1"><br><br>
<input type="Submit" value="Upload File" onclick="showProgress()" method="POST">

<script>
function showProgress() {
    if(document.frm.file1.value == "")
    {
    alert('Please upload a file');
    }       
    else
    {       
    document.getElementById('progress').style.display = 'block';
    }
    }
</script>

在我的jsp文件中,我有上面的代碼。 如果未選擇任何文件,則成功顯示alert('Please upload a file') 但是它將調用Servlet程序Readcsvv並轉到下一頁。

在顯示警報框后,我希望編程位於同一頁面。 我該怎么辦?

<input type="Submit" value="Upload File" onclick="return showProgress()" method="POST">

將其與return false一起使用

如果是jQuery,則必須在onClick事件處理程序的末尾返回false。 我會嘗試return false;

您還需要將onclick屬性更改為form元素上的onsubmit屬性。

嘗試這個:

<form name='frm' action="Readcsvv" enctype="multipart/form-data" method="POST" onSubmit="return false;"> 

試試下面,

<form name='frm' action="Readcsvv" enctype="multipart/form-data" method="POST" > 
<input type="file" name="file1"><br><br>
<input type="Submit" value="Upload File" onclick="return showProgress();" method="POST">

<script>
function showProgress() {
    if(document.frm.file1.value == "")
    {
    alert('Please upload a file');
    return false;
    }       
    else
    {       
    document.getElementById('progress').style.display = 'block';
    return true;
    }
    }
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM