Can I use two events in a single line?
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.
精彩评论