php - jQuery form plugin, how to prevent the form submit -
i using jquery form plugin upload file , show progress bar.
the js code :
var bar = $('.bar'); var percent = $('.percent'); var status = $('#status'); var options = { target: '.result', url: 'slider_insert_now.php', beforesend: function() { var img_file = $.trim($(".img_file").val()); if(img_file==$.trim('')){ $(".result").html('<span class="error">no file selected</span>'); return false; }else{ status.empty(); var percentval = '0%'; bar.width(percentval) percent.html(percentval); return true; } }, uploadprogress: function(event, position, total, percentcomplete) { var percentval = percentcomplete + '%'; bar.width(percentval) percent.html(percentval); }, success: function() { var percentval = '100%'; bar.width(percentval) percent.html(percentval); }, complete: function(xhr) { status.html(xhr.responsetext); } }; $('#form_upload').ajaxform(options);
and html:
<div class="result"></div> <form id="form_upload" action="javascript:void(0)" method="post" enctype="multipart/form-data"> <div class="progress"> <div class="bar"></div> <div class="percent">0%</div> </div> <div id="status"></div> <input type="file" value="" name="img_file" class="img_file" /> <br></br> <input type="submit" value="upload image" /> </form>
in beforesend
function, used return false
prevent form submitting itself. not work . how can achieve that?
in html remove javascript:void(0) , put # instead, , use event.preventdefault(); ever trigger submit. this:
$(document).on('click', '#submitname', function (event) { event.preventdefault(); $.ajax({ url: 'somepage.php', type: 'post', datatype:'html', data: $('#formname').serialize(), success: function(response, textstatus, jqxhr){ ///do }, error: function(jqxhr, textstatus, errorthrown){ console.log('error(s):'+textstatus, errorthrown); } }); });
Comments
Post a Comment