开发者

How to check change event fired or not in the jquery

开发者 https://www.devze.com 2023-01-04 01:34 出处:网络
How to check my change event fired or not if its fird I need send true if not i need to send false.. $(\"#selectid\").chan开发者_StackOverflow中文版ge(function(){

How to check my change event fired or not if its fird I need send true if not i need to send false..

$("#selectid").chan开发者_StackOverflow中文版ge(function(){
});

I need to check this event is fired or not..?

thanks


I assume this is related to your other question. I updated my answer in that one since you asked about it there, but I guess I'll post the answer here too.

One way to track which form elements were modified would be through .data().

var array;

$("#fieldset").change(function(event) {
    $(event.target).data('changed',true);
});

$("form").submit(function() {
    array = $(this).find("input, select,textarea").map(function() {
        var $th = $(this);
        if( $th.data('changed') ) return $(this).val();
    }).get();

    alert(array);
    return false;
});​


var didF=false;

$("#selectid").change(function(){
   didF=true;
});

...

if (didF) {
   ...
}


The line of code you included in your question binds a function (a callback or handler) to the change event of the element with ID selectid. Are you trying to determine if your callback fires?

If that's the case, there are a variety of options. If you're using Firebug or another debugger, you can either put a breakpoint somewhere in your callback. You can also use the debugger; keyword to "hardcode" a breakpoint into a specific location, like this:

$("#selectid").change(function(){
    debugger;
});

The most rudimentary way to see when your callback fires is to use an alert in place of the debugger statement:

$("#selectid").change(function(){
    alert("It fired!";
});

I'm not sure what you mean about "sending true or false."


You can't reliably check if an event has fired from outside the handler.

I assume you're looking for something such as:

var changed = false;

$("#selectid").change(function(){
    changed = true;
});

if (changed) {
    doSomething();
} else {
    doSomethingElse();
}

But, unless the user is insanely fast (and the browser isn't faster still), the if will be evaluated long before the change handler function is actually called. So, doSomethingElse will be called 99.99999999999999% of the time.

However, you can factor that same human input delay into your approach. Rather than post-testing for changes, assume from the start that no changes have occurred. Then, contain your true script in the change handler. But, the key is to handle all dependencies of the change event within the handler.

prepareWithoutChanges();

$("#selectid").change(function(){
    handleChange();
    triggerOtherDependencies();
});

But, I'm afraid, without knowing what you need to accomplish, I can't be anymore specific than this.

0

精彩评论

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