日期:2014-05-16  浏览次数:20404 次

怎样能在提交表单时过滤掉没更改的值,只POST修改过的值?
假如有个网站配置的表单里面有超过二十个的值,有时候只更改了一两个值,如果全部POST的话来更新的话会造成效率很低,能不能在提交时就判断,如果没更改过的值就不提交了?javascript和jquery都略懂一点

------解决方案--------------------
估计你想要的功能应该是这样的
HTML code
<script  type="text/javascript">
function IsFormChanged(el,radios) {  
    var isChanged="";  
    var form = document.getElementById(el);  
    for (var i = 0; i < form.elements.length; i++) {  
        var element = form.elements[i];  
        var type = element.type; 
        switch(type){
            case "text":
            case "hidden":
            case "textarea":
            case "button":
                isChanged += (element.value != element.defaultValue)?isChanged.length==0?element.name+"="+element.value:"&"+element.name+"="+element.value:"";
                break;
            case "checkbox":
                isChanged += (element.checked != element.defaultChecked)?isChanged.length==0?element.name+"="+element.value:"&"+element.name+"="+element.value:"";
                break;
            case "select-one":
            case "select-multiple": 
                for (var j = 0; j < element.options.length; j++) {
                    isChanged += (element.options[j].selected != element.options[j].defaultSelected)?isChanged.length==0?element.name+"="+element.value:"&"+element.name+"="+element.value:"";  
                }
                break;  
        } 
    }   
    var radiolist=radios.split(',');
    for(var j=0;j<radiolist.length;j++){
        var myRadio=document.getElementsByName(radiolist[j]);
        for(var k=0;k<myRadio.length;k++){
            isChanged += (myRadio[k].checked && !myRadio[k].defaultChecked)?isChanged.length==0?myRadio[k].name+"="+myRadio[k].value:"&"+myRadio[k].name+"="+myRadio[k].value:"";
        }
    }
    alert(isChanged);  
} 
</script>
<form id="chageform">
    <input type="checkbox" name="myBox" value="1"/>
    <input type="checkbox" name="myBox" value="2"/>
    <input type="checkbox" name="myBox" value="3"/><br />
    <input type="radio" name="myRadio" checked="checked" value="1"/>
    <input type="radio" name="myRadio" value="2"/><br />
    <input type="radio" name="myRadio2" value="1"/>
    <input type="radio" name="myRadio2" checked="checked" value="2"/>
<input type="button" value="检测" onclick="IsFormChanged('chageform','myRadio,myRadio2')" />
</form>