8

我有一个文本字段,当用户从选择字段中选择一个选项或在文本字段中输入文本并点击链接时,我需要设置该值。然后,这两个事件都会关闭包含选择和文本字段/链接的 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();
});
4

6 回答 6

2

无法复制

我已经摆弄了这个并且无法复制它:http: //jsfiddle.net/DCM48/

请上传有关该问题的完整小提琴以获得进一步的帮助。

-莱德杰

于 2012-10-17T21:30:22.477 回答
1

尝试使用委托

$(document).ready(function() {
    $("body").delegate("click", ".text_choice_button", function() {
           your code......
    });
});
于 2012-09-03T10:02:44.897 回答
0

我对'onclick'处理程序usign jquery有问题。我发现 mousedown 是一个更可靠的解决方案。我发现可以更好地捕捉点击次数。http://api.jquery.com/mousedown/

于 2012-10-19T09:40:53.727 回答
0

我总是使用这种模式,也许你也应该尝试一下。

$(element).click(function() {});

还要在此处发布您使用 javascript 的 html 代码。

于 2012-11-01T13:14:05.567 回答
0
$(function(){
    $("body").click("a, div", function(){
        $("body").append("ping");
    });
});
于 2012-11-04T10:34:16.240 回答
-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();
});
于 2012-05-16T15:03:16.750 回答