使用javascript进行Codeigniter弹出验证

时间:2017-01-06 06:57:11

标签: javascript codeigniter

<form action="<?php echo base_url();?>test/loadsql"  method="post" enctype="multipart/form-data"> 
                    <input type="file" required aria-required="true" id="upload" name="upload"/>
                    <input type="button" onclick="validate()"  class="btn btn-success " value= "Upload">
                    </form>

function validate()
{
    job=confirm("Are you sure to upload?");
    if(job!=true)
    {
        return false;
    }
}
</script>

如果真的我想加载表单动作test / loadsql

请帮助

2 个答案:

答案 0 :(得分:0)

更改按钮类型submit而不是button并声明点击功能return对于返回功能非常重要

更新的答案验证输入

function validate()
{
  var input =document.getElementById('upload').value;
   if(input){
      var  job=confirm("Are you sure to upload?");
    if(job!=true)
    {
      console.log('not allow')
        return false;
    }
  else{
    console.log("pass")
      return true;
    }
    }
  else{
     alert('Please choose some file')
    }
}
<form action="<?php echo base_url();?>test/loadsql"  method="post" enctype="multipart/form-data"> 
                    <input type="file" required aria-required="true" id="upload" name="upload"/>
                    <input type="submit" onclick="return validate()"  class="btn btn-success " value= "Upload">
                    </form>

答案 1 :(得分:0)

在表单上使用onsubmit()事件,如下所示。

查看:

<form action="<?php echo base_url('test/loadsql');?>"  method="post" enctype="multipart/form-data" onsubmit="validate();"> 

<input type="file" required aria-required="true" id="upload" name="upload"/>
<input type="submit" class="btn btn-success" value= "Upload">

 </form>

的Javascript

function validate()
{
    if(window.confirm("Are you sure to upload?"))
    {
        return true;
    }
  else{
      return false;
    }
}