最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Finding Changed Form Element With jQuery - Stack Overflow

programmeradmin1浏览0评论

i have a form(id="search_options") and i tracking changes in form by:

    $("#search_options").change(function() { 
        // Bla Bla Bla
    });

In this form i have many inputs and one of is select(id="project_category") and i want to catch if user changed project_category select or another input. How can i done this ? Thanks

i have a form(id="search_options") and i tracking changes in form by:

    $("#search_options").change(function() { 
        // Bla Bla Bla
    });

In this form i have many inputs and one of is select(id="project_category") and i want to catch if user changed project_category select or another input. How can i done this ? Thanks

Share Improve this question asked Jun 8, 2010 at 20:54 mTuranmTuran 1,8345 gold badges35 silver badges59 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

What you are looking to do could be acplished 2 ways I can think of off the top of my head. First, Capture the change event for all input items in your form individually

$("#search_options input,#search_options select").change(function() { 
   // "this" is the input that has changed so you would check if it had id = "project_category" here        
});

Alternatively you could track the current value of the "project_category" on every change

var current_Project_Category = ""; 
$("#search_options").change(function() { 
        if(current_Project_Category != $(this).find("#project_category").val())
        { 
           //project category has changed so mark this as the current one 
           //also run any change code here
           current_Project_Category = $(this).find("#project_category").val()   
        }

    });

There is a plugin made for exactly this purpose:

jQuery Form Observe

This plugin observes values of form elements. When end-user changes any values of input elements, observer shows which values were changed. And observer also alerts to users when they try to move out from the page before submitting changes.

Try this instead:

$("#search_options").find('input,select').change(function() {
    var changedId = $(this).attr('id');
    var newVal = $(this).val();
});
发布评论

评论列表(0)

  1. 暂无评论