Home >Web Front-end >JS Tutorial >Jquery determines whether the form data has changed_jquery
The example in this article shares the three major steps for Jquery to determine whether the form data has changed for your reference. The specific content is as follows
1. The idea is: Click a row of datagrid when modifying, and perform page jump data binding. When submitting the form, serial number form form, and re-obtain the selected row data of datagrid, and perform data Compare. 54bdf357c58b8a65c66d7c19c8e4d114
/** * 判断form内属性值是否被修改 * * @param jsonForm 对应修改的form序列化后的json数据 * @param row 对应datagrid选中的数据源 * * @Return true 存在修改项 * false 不存在修改项 */ isModified:function(jsonForm,row){ for(key in jsonForm){ //form中存在,但数据源datagrid中不存在 if(row[key] == undefined){ continue; } //form中""或null,但数据源datagrid中为null或""时,不进行比较 if(!((jsonForm[key] == null || jsonForm[key] == '') && (row[key] == null || row[key] == ''))){ //如果值不同,则返回true if(jsonForm[key] != row[key]) return true; } } return false; }
2. Obtain the change information before and after the changed value. Since the project needs to record the value change record, and save the Chinese and English information of the column, as follows:
/** * 获取form被修改信息 * * @param jsonForm 对应修改的form序列化后的json数据 * @param row 对应datagrid选中的数据源 * @param columnJSModel 为表单form中name属性中英对照js,需在update.jsp中引用此js文件 * * @Return json 数据如:[{"updateColumn":"loanRatio","updateColName":"额比例","updateContent":"由11变更为100.00"}, * {"updateColumn":"loanQuotaTypeName","updateColName":"比例形式","updateContent":"由 卡 变更为 实物"}] * */ getModifiedField:function(jsonForm,row,columnJSModel){ var modifiedArry = []; var jsonArrStr = ''; for(key in jsonForm){ //form中存在,但数据源datagrid中不存在 if(row[key] == undefined){ continue; } //form中""或null,但数据源datagrid中为null或""时,不进行比较 if(!((jsonForm[key] == null || jsonForm[key] == '') && (row[key] == null || row[key] == ''))){ //如果值不同,则返回true if(jsonForm[key] != row[key]){ var jsonObj = {}; jsonObj.updateColumn = key; jsonObj.updateColName = columnJSModel[key]; jsonObj.updateContent = '由'+ row[key] + '变更为' + jsonForm[key]; modifiedArry[modifiedArry.length] = jsonObj; } } } jsonArrStr = JSON.stringify(modifiedArry); //console.log(jsonArrStr); return jsonArrStr; }
3. The columnJSModel is a Chinese-English JS entity, and ratio and qutaTypeName respectively correspond to the name attribute name in the form, as follows:
var policyColumn = { ratio : '比例', quotaTypeName : '比例形式' };
The above is the entire content of this article, I hope it will be helpful to everyone’s study.