简体   繁体   中英

jquery using .submit() form not submitting

I am submitting a form using jQuery but when I use JavaScript it is submitted but through jQuery it is not submitting.

Here is my form code:

<form method="post" id="uploadfrm" action="<?php echo site_url('filemanager/upload'); ?>" enctype="multipart/form-data" >
<div class="up_wraper">Click here to Select a File </div>
<input type="file" name="upload[]" id="upload" multiple="multiple" /> </form>   

jQuery code:

<script type="text/javascript">
   $(document).ready( function() {
       $("#upload").change( function() { 
        var value=$("#foo").val();
        var file=$("#upload").val();
        var arr = value.split("_");
        var id = arr[1];
        var type = arr[0];

        if(type!='folder'){
            alert('Select a folder to upload files..!');
            }
          else{
                 $("#foo").val(id);
                 $('#uploadfrm').submit( function(event) {
                 event.preventDefault();

                 });

            }
       });
   });
</script>

When I use in else condition:

document.forms["uploadfrm"].submit();

it submits successfully.

But I want to submit it using jQuery using preventDefault() method.

In your code you have this...

$('#uploadfrm').submit( function(event) {
    event.preventDefault();
});

The 2nd line, event.preventDefault(); , stops the form submitting. If you remove that it will submit fine. Just do this...

$('#uploadfrm').submit();

You don't need event.preventDefault(); Simple $('#uploadfrm').submit(); triggers form submission. However, if you want to process some validations before submission, you can add another $.submit(); before your submission trigger.

    $('#uploadfrm').submit(function(){
      //your code goes here. 
     //return false; //if you want to break submission on failed validation, or if you want to use ajax callback
     //return true; // if you want to submit with regular form request. 
    });

   //this line bellow goes in your conditional, while upper one goes out of your $('#upload').change(); //event
   $('#uploadfrm').submit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM