开发者

How can I partially reset a form with jquery?

I have a form whitch divided into a few divs. I use jquery to display related part and hide that part to show the next one when next button of the page is clicked.

Since it is a partial form, I need to reset only part of the form.

User selects a category, then go to next page, selects a sub-category and continue. Next page ise an application form, but user decides to change the sub-category, so he goes back a page and change the subcategory.

开发者_运维问答

In that case, form needs to reset te application form part, but selected category must be keept unchanged.

The form is divided info different parts with divs and tables whose id is used in jquery events. But I can not create different forms for each page.


So, you do not want to reset those sections using server side logic? Am I correct? Assuming that you are able to detect the page from where user is coming, and only need to reset the appropriate part, following is how I would think:-

Define functions to fetch sub-sections (for resetting) like this:-

//These two functions return the ids of the divs which need to be ignored
function getApplicationPart()
{
  //Assuming there are multiple sections to be reset
  var sections = ["Banana", "Orange", "Apple", "Mango"];
  return sections;
}

function getSubCategoryPart()
{
  var sections = ["Potato", "Onion"];
  return sections;
}

And define one function which will do the necessary resetting

function resetSections(sectionsArray)
{
    $.each(sectionsArray, function(index, value) { 
        //reset the values of all child input tags (inputs inside the section holding div)
        $("#"+value).children("input").val("");
    });
    //define any other hiding thing you need to do here
}

Now, you just have to decide which sections to be reset and call the resetSections() function accordingly. like this:-

if((subcategoryPage)&&(comingfromApplicationPage))
{
     resetSections(getApplicationPart());
}

Hope this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜