JS/Jquery 表单方式提交总结

1. submit提交

(1). submit 按钮式提交

缺点:在提交前不可修改提交的form表单数据

// 1. html
<form method="post" action="/../.." >
    <input type="text" name="username" value="" />
    <input type="password" name="pwd" value="" />
    <input type="submit" value="提交"/>
</form>

(2). onsubmit方式提交

优点:在请求提交操作(action)时,可对提交的数据进行处理

// 1. html
<form id='test_form' action='' method='' onsubmit='return checkForm()'>
    <input type='text' name='username' value=''/>
    <input type='password' id='input_pwd' value =''/> // 注意此没有name属性,不会提交
    <input type='hidden' name='pwd' id='md5_pwd' value=''/> // 此有name属性,会被form表单提交
    <button type='submit'>提交<button/>
</form>
// 2.js
function checkForm () { // 点击“提交”按钮,执行的方法
    var input_pwd= document.getElementById('input_pwd');
    var md5_pwd= document.getElementById('md5_pwd');
     md5_pwd.value= toMD5(input_pwd.value);
    //进行下一步
    return true;
}

2. formData 提交

// 1. html
<form id="photoForm">
     <input id="photoInput" type="file" title="图片上传" accept=".jpg,.jpeg,image/jpg,image/jpeg" name="myfiles" multiple="multiple">照片导入
</form>

// 2. js
$('#photoForm input').change(function() {
    var photoForm = $('#photoForm')[0],
    photoFormData = new FormData(photoForm),
    photoFileList = $('#photoInput')[0].files; // 上传的文件列表
    $('.loading').show();
    $.ajax({
        type: 'POST',
        url: ZD.url+"/cert/filesUpload",
        data: photoFormData,
        // 告诉jQuery不要去处理发送的数据
        processData : false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType : false,
        complete:function(){
           $('.loading').hide();
           $("#photoForm input").val('');
        },
        success:function(d){
            // 成功。。
       }
     });
});

3. 动态添加表单提交, js方式提交

1. 动态追加的form表单

var exportForm = $('<form action="'+ajaxUrl+'" method="post">    <input type="hidden" name="beginDate" value="'+$(".beginDate").val()+'"/>    </form>');
  $(document.body).append(exportForm);
   exportForm.submit(); // 表单提交
   exportForm.remove();

2. 静态form表单,js方式提交

// 1. html
<form action="'+ajaxUrl+'" method="post">
    <input type="hidden" name="beginDate" value="'+$(".beginDate").val()+'"/>
</form>

// 2. js/JQuery
document.getElementById("form").submit(); // js写法
$("#form").submit(); // jquery写法

原文地址:https://www.cnblogs.com/zero-zm/p/11622779.html

时间: 2024-12-09 21:53:30

JS/Jquery 表单方式提交总结的相关文章

js 防止表单重复提交

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title&

js日期/时间比较函数,以及js校验表单后提交表单的三种方法,表单验证,以及三种结合

<pre class="html" name="code"> js日期比较(yyyy-mm-dd) function duibi(a, b) { var arr = a.split("-"); var starttime = new Date(arr[0], arr[1], arr[2]); var starttimes = starttime.getTime(); var arrs = b.split("-");

jQuery——表单异步提交

如果不做任何处理,表单提交时会刷新页面,为了改善体验,可以使用jQuery做到异步提交表单:通过$("#form").serialize()将表单元素的数据转化为字符串,然后通过$.ajax()执行异步请求资源. 网页代码: <form mothod="POST" id="search_form"> <div class="cf"> <label class="search-bar&qu

js创建表单并提交

1.脚本 Util = { post : function(URL, PARAMS){ //虚拟表单实现post提交 var temp = document.createElement("form"); temp.action = URL; temp.method = "post"; temp.style.display = "none"; for (var x in PARAMS) { var opt = document.createElem

js阻止表单重复提交

//校验表单的数据 function newFatherModuleVerify() { var moduelName = $('#fatherModule_moduelName').val(); alert(moduelName); return false; } <form:form commandName="fatherModule" action="saveFatherModule" onsubmit="return newFatherMod

JS防止表单重复提交

html代码 <form action="booking.php" method="post" name="chkForm" id="chkForm" > 姓名:<input name="real_name" type="text" id="real_name" class="inputBg" size="20"

js form 表单的提交

$("#confirm").click(function(){    var url = "${basePath}${path}saverpGoodsbase";  $("#form_item").attr("action", url);  $("#form_item").submit();   }); }; $("#form_item").attr("action"

Ext js form表单的提交

//定义的文本框 var GangKouCode = { xtype: 'textfield', name: 'PORTCODE', id: 'PORTCODE', allowBlank: false, emptyText: '请输入编号', blankText: '请输入编号', fieldLabel: '港口编号', labelAlign: 'right', labelWidth: 60, width: 150, autoFitErrors: true, msgTarget: 'side'

js验证表单并提交

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title></title> <meta http-equiv="content-type" content=