我再次提出另一个问题。
功能执行此操作:
我的问题是,我不确定如何附加按键13和点击事件,以便不必两次写相同的功能,请帮忙吗?
$(document).on("click", '.edit_qty_button', function() {
var qty = $(this).parent();
qty.html('<input class="qty_input_box" type="text" /><br><span class="done_editing_button">Done</span>');
$('.done_editing_button').on('click', function() {
var new_qty = $('.qty_input_box').val();
qty.html(new_qty + '<br><span class="edit_qty_button">Edit</span>');
});
$('.qty_input_box').keypress(function(e) {
if (e.which == 13) {
var new_qty = $('.qty_input_box').val();
qty.html(new_qty + '<br><span class="edit_qty_button">Edit</span>');
}
});
});
答案 0 :(得分:1)
试试这个
function myFunction() {
var new_qty = $('.qty_input_box').val();
qty.html(new_qty + '<br><span class="edit_qty_button">Edit</span>');
}
$('.done_editing_button').on('click', myFunction);
$('.qty_input_box').keypress(function(e) {
if (e.which == 13) {
myFunction();
}
});
答案 1 :(得分:1)
语法可能有误,但基本上你从两个不同的事件中调用相同的函数:
$('qty_input_box').onkeypress=checkKeyFunc; // Capture keypress & go off to check for ENTER
// Capture the button click event
$('done_editing_button').on("click", '.edit_qty_button', function() {TransformsCurrentNumberFunction};
// Function to check if keypress was ENTER
function checkKeyFunc()
{
if (window.event.keyCode == 13)
{
TransformsCurrentNumberFunction;
}
}
// Function to implement shared logic to run on CLICK or ENTER events
function TransformsCurrentNumberFunc() {
// Implement your shared logic in this function
}
BTW ...与Firefox或Chrome相比,您可能需要在IE中以不同的方式处理按键。 http://forums.codeguru.com/showthread.php?370260-Detect-keypress-of-ENTER