Jquery .on点击第一次点击不工作

Gab*_*e K 8 forms jquery click

我有一个文本字段,我需要设置当用户从选择字段中选择一个选项或在文本字段中输入文本并点击链接时的值.然后这两个事件都会关闭包含select和text字段/链接的div.

选择选项有效

$(document).on("change", ".select_choice", function(event) {
    var string = $(this).find("option:selected").text();
    $(this).closest(".select_toggle_area").find(".toggle_choices").toggle();
    $(this).closest(".select_toggle_area").find(".target_input").val(string);
});
Run Code Online (Sandbox Code Playgroud)

文本字段/链接在第二次单击时起作用.无论何时在字段中输入内容,链接在第一次单击时都不起作用.它隐藏div,但不更新目标字段的值.

$(document).on("click", ".text_choice_button", function(event) {
    event.preventDefault();
    var string = $(this).closest(".select_toggle_area").find(".text_choice").val();
    $(this).closest(".select_toggle_area").find(".target_input").val(string);
    $(this).closest(".select_toggle_area").find(".toggle_choices").toggle();
});
Run Code Online (Sandbox Code Playgroud)

小智 -3

用“live”试试

$(".text_choice_button").live('click', function(event) {
    event.preventDefault();
    var string = $(this).closest(".select_toggle_area").find("text_choice").val();
    $(this).closest(".select_toggle_area").find(".target_input").val(string);
    $(this).closest(".select_toggle_area").find(".toggle_choices").toggle();
});
Run Code Online (Sandbox Code Playgroud)