开发者

Conditionally skip form fields from submission, Reduce URL clutter

I have multiple hidden form fields which store values about the the current view (e.g. if certain, normally hidden div's are visible etc.) to restore the layout when the form posts back.

The problem is that I'm al开发者_运维问答ways submitting all these hidden fields, even if they are in default, generating lots of unnecessary URL clutter in the process (e.g. http://www.example.com/view?ab=&ac=&ad= and so on).

I'd rather submit only the fields which are actually influencing the view (meaning, don't have a specified default value) so that the URL clutter is at a minimum.

I tried manually deleting/inserting input's but its a nightmare. Is there a better way to do this?


Using jQuery, you might just remove() those form elements before transmitting. Another way I could think of is to remove the name attribute.

$('form').bind('submit', function(){
    $(this).children('input').each(function(){
        if(this.value === this.defaultValue)
           $(this).remove();
    });
});


Try disabling them-
"Controls that are disabled cannot be successful." -- http://www.w3.org/TR/html401/interact/forms.html#h-17.13.2


Unless you have a reason for keeping these options in the URL, why not store those values in a cookie? In fact, this is often done on many sites. If you want to be more careful, the display options can be stored in a database for each user, but that's your choice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜