开发者

Can I use two events in a single line?

开发者 https://www.devze.com 2023-03-22 12:05 出处:网络
I want an action to take place on keyup as well as on change. The statement below works perfectly for the keyup event.I want to duplicate it on change.

I want an action to take place on keyup as well as on change.

The statement below works perfectly for the keyup event. I want to duplicate it on change.

Is there an easy way to accomplish this?

$("#YourName").keyup(function() {
    ThisValue = $(this).attr("value");
    if (ThisValue.length > 0) {
        ThisValue = "<b>From:</b><br /> " + ThisValue;
        $("#YourNa开发者_开发问答meValid").html(ThumbsUp);
    } else {
        ThisValue = "";
        $("#YourNameValid").html("");
    }
    $("#YourNameText").html(ThisValue);
});


Try this

$("#YourName").bind('keyup change', function() {
    ThisValue = $(this).val();
    if (ThisValue.length > 0) {
        ThisValue = "<b>From:</b><br /> " + ThisValue;
        $("#YourNameValid").html(ThumbsUp);
    } else {
        ThisValue = "";
        $("#YourNameValid").html("");
    }
    $("#YourNameText").html(ThisValue);
});


function yourEventHandler() {
    ThisValue = $(this).attr("value");
    if (ThisValue.length > 0) {
        ThisValue = "<b>From:</b><br /> " + ThisValue;
        $("#YourNameValid").html(ThumbsUp);
    } else {
        ThisValue = "";
        $("#YourNameValid").html("");
    }
    $("#YourNameText").html(ThisValue);
}

$("#YourName").keyup(yourEventHandler).change(yourEventHandler);

The above uses a common function passed as the event handler to each event. Alternatively, you can use bind (see docs), which accepts multiple events as the first parameter.

0

精彩评论

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