简体   繁体   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>

In my jsp file I have the above code. 在我的jsp文件中,我有上面的代码。 If there is no file selected the alert('Please upload a file') is displayed successfully . 如果未选择任何文件,则成功显示alert('Please upload a file') But it calls the servlet program Readcsvv and goes to the next page. 但是它将调用Servlet程序Readcsvv并转到下一页。

After diplaying the alert box i want to program to be in same page. 在显示警报框后,我希望编程位于同一页面。 What should I do for that? 我该怎么办?

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

将其与return false一起使用

If this were jQuery you would have to return false at the end of an onClick event handler. 如果是jQuery,则必须在onClick事件处理程序的末尾返回false。 I would try return false; 我会尝试return false; .

You also need to change your onclick attribute to be an onsubmit attribute on the form element. 您还需要将onclick属性更改为form元素上的onsubmit属性。

尝试这个:

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

Try below, 试试下面,

<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