我有一个文本字段,当用户从选择字段中选择一个选项或在文本字段中输入文本并点击链接时,我需要设置该值。然后,这两个事件都会关闭包含选择和文本字段/链接的 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);
});
文本字段/链接在第二次单击时起作用。任何时候在该字段中输入某些内容时,链接在第一次单击时都不起作用。它隐藏了 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();
});