【问题标题】:Can we combine two function in jquery with different event type我们可以将jquery中的两个函数与不同的事件类型结合起来吗
【发布时间】:2014-07-30 01:50:44
【问题描述】:

无论如何我可以结合这个:

$("tbody").on("keyup", "tr td:nth-child(2) input,td:nth-child(3) input,td:nth-child(4)",
function () {
    $(this) //input
    .closest("tr") //finds the row associated with the input
    .find("td:first input") //returns the first cell
    .val(this.value); //sets the text
});

$('select').on('change', function() {
    $(this).closest("tr").find("td:first input").val( this.value );
});

对于select changekeyup for input 仅适用于一个函数,例如:

$("tbody").on("input keyup select change", "tr td:nth-child(2) input,td:nth-child(3) input,td:nth-child(4)",
...
});

我也想像type=text 的第一个输入一样设置它:

$(this).closest("tr").find("td:first input[type="text"]").val( this.value );

但它不起作用。

fiddle here

【问题讨论】:

    标签: jquery select input onchange onkeyup


    【解决方案1】:

    试试这个:

    $("tbody").on("keyup change", "tr td:nth-child(2) input,td:nth-child(3) input,td:nth-child(4) select",
    function () {
        $(this) //input
        .closest("tr") //finds the row associated with the input
        .find("td:first input") //returns the first cell
        .val(this.value); //sets the text
    });
    

    DEMO

    【讨论】:

    • 哇,我想我以前尝试过这种组合,但没有注意到它有效。哈哈 txt 先生@Unknown
    • 但是我不能只在第一次输入文本类型时使用它。你能帮我吗
    • .find("td:first input['type=text']").val(this.value);这不起作用
    • 它应该可以工作。这工作正常/您仍然面临问题吗?
    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 2018-07-27
    • 2015-10-11
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多