I have a form that uses a dash of Jquery to calculate a ticket price by number of tickets needed by cost of ticket. Very basic.
The number of tickets element is a dropdown开发者_运维百科 menu (select) and works by using Jquery's click method. This works fine.
The issue is the user can also use the key up and down keys on their keyboard. So I copy verbatim, my code and use the blur method instead of click.
My question is, can JQuery have click + blur together instead of copying code?
Example
var ticket_price = $('.fee').text();
$('input#amount').attr('value', ticket_price);
$('table#booking-table select').blur(function()
{
var ticket_number = $(this).val();
var ticket_price = $('.fee').text();
var total_price = ticket_number * ticket_price;
total_price = total_price.toFixed(2);
var input_total_price = $('input#amount').attr('value', total_price);
if(ticket_number == 1)
{
$('tr.ticket-price td.right span.total_fee').empty().append();
}
else
{
$('tr.ticket-price td.right span.total_fee').empty().append('* ' + ticket_number + ' tickets = ' + '<strong>€' + total_price + '</strong>');
}
});
You can use
$(selector).bind('click blur')
Or if you use jQuery 1.4:
$('#foo').bind({
click: function() {
doSomething();
},
blur: function() {
doSomething();
}
});
function doSomething() {
var ticket_number = $(this).val();
var ticket_price = $('.fee').text();
// etc ...
}
精彩评论