Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript阻止上传窗口弹出_Javascript_Jquery_Html_Upload - Fatal编程技术网

使用javascript阻止上传窗口弹出

使用javascript阻止上传窗口弹出,javascript,jquery,html,upload,Javascript,Jquery,Html,Upload,如果只输入了输入值,那么用户应该能够上传文件。但是,只要我点击按钮,上传文件的窗口就会弹出。我不希望它弹出,除非用户在下面所述的输入文件中输入了值。 这些是输入: <input type="text" id="id" placeholder="Enter Audit ID" autocomplete="off"> <input type="text" id="email_id_upload" placeholder="Enter Email_id " autocomplete

如果只输入了输入值,那么用户应该能够上传文件。但是,只要我点击按钮,上传文件的窗口就会弹出。我不希望它弹出,除非用户在下面所述的输入文件中输入了值。 这些是输入:

<input type="text" id="id" placeholder="Enter Audit ID" autocomplete="off">
<input type="text" id="email_id_upload"  placeholder="Enter Email_id " autocomplete="off">

这是上传程序:

<form>
    <input type="file" name="file" id="file"><br>
    <input type="button" name="submit_file" value="Submit">
</form>


这是检查用户是否输入了所有值的Js:

   $('#file').click(function(){

        var email_id_upload= $('#email_id_upload').val();
      var id =  $('#id').val();
        if(email_id_upload.length !==0 &&  id.lenght !==0)
        {
        //allow upload window to pop up

         }
        else
            {
                if(email_id_upload.length ===0 && id.length ===0)
                {$('#message_content2_1').html("<span style='color:red'>Please fill email id and audit id</span>");}
                else{
                if(email_id_upload.length ===0)
                {$('#message_content2_1').html("<span style='color:red'>Please fill email id</span>");}
                if(id.length ===0)
                {$('#message_content2_1').html("<span style='color:red'>Please fill email id and audit id</span>");}
                }
            }
    });
$('#文件')。单击(函数(){
var email_id_upload=$('#email_id_upload').val();
var id=$('#id').val();
if(email_id_upload.length!==0&&id.lenght!==0)
{
//允许上传窗口弹出
}
其他的
{
if(email\u id\u upload.length==0&&id.length==0)
{$('#message_content2_1').html(“请填写电子邮件id和审核id”);}
否则{
如果(email\u id\u upload.length==0)
{$('#message_content2_1').html(“请填写电子邮件id”);}
如果(id.length==0)
{$('#message_content2_1').html(“请填写电子邮件id和审核id”);}
}
}
});

你发布JS的方式实际上对我很有用。if条件中有一个输入错误:
id.lenght
。通常,只绑定到click事件不是一个好主意,因为有人也可能通过在表单字段中按enter键触发submit事件(例如submit事件)


在您的示例中,当两个字段都为空且有人单击#file元素时,将执行else块,单击事件将冒泡到#file元素的容器中。要停止事件冒泡,请在else块末尾返回false。

I,只需更正您的脚本即可。我认为这是可行的:

$('#file').click(function(e){

    var email_id_upload= $('#email_id_upload').val();
  var id =  $('#id').val();
    if(email_id_upload.length !==0 &&  id.lenght !==0)
    {
    //allow upload window to pop up

     }
    else
        {
            e.preventDefault();
            if(email_id_upload.length ===0 && id.length ===0)
            {$('#message_content2_1').html("<span style='color:red'>Please fill email id and audit id</span>");}
            else{
            if(email_id_upload.length ===0)
            {$('#message_content2_1').html("<span style='color:red'>Please fill email id</span>");}
            if(id.length ===0)
            {$('#message_content2_1').html("<span style='color:red'>Please fill email id and audit id</span>");}
            }
        }
});
$(“#文件”)。单击(函数(e){
var email_id_upload=$('#email_id_upload').val();
var id=$('#id').val();
if(email_id_upload.length!==0&&id.lenght!==0)
{
//允许上传窗口弹出
}
其他的
{
e、 预防默认值();
if(email\u id\u upload.length==0&&id.length==0)
{$('#message_content2_1').html(“请填写电子邮件id和审核id”);}
否则{
如果(email\u id\u upload.length==0)
{$('#message_content2_1').html(“请填写电子邮件id”);}
如果(id.length==0)
{$('#message_content2_1').html(“请填写电子邮件id和审核id”);}
}
}
});

在单击功能中添加
返回false
,以阻止单击操作