【问题标题】:Jquery val don't set new value to a textboxJquery val不为文本框设置新值
【发布时间】:2013-02-13 10:07:55
【问题描述】:

我使用 jqueryUI 自动完成来搜索地理名称,但我无法为我的文本框设置值 这是我的代码:

<script>
    $(function () {
        function log(message) {
            $("#appendedInputButton").val(message);
        }
        $("#appendedInputButton").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "http://ws.geonames.org/searchJSON",
                    dataType: "jsonp",
                    data: {
                        featureClass: "P",
                        style: "full",
                        maxRows: 12,
                        name_startsWith: request.term
                    },
                    success: function (data) {
                        response($.map(data.geonames, function (item) {
                            return {
                                label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                                value: item.name
                            }
                        }));
                    }
                });
            },
            minLength: 1,
            select: function (event, ui) {
                log(ui.item ?
                    "Selected: " + ui.item.label :
                    "Nothing selected, input was " + this.value);
            },
            open: function () {
                $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
            },
            close: function () {
                $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
            }
        });
    });
</script>     

我的 HTML 是:

<div class="input-append">
    <form role="search" method="get" id="searchform" action="http://sushiant.com/">
        <button class="btn" type="submit" id="searchsubmit">جستجو</button>
        <input class="span2" name="appendedInputButton" id="appendedInputButton" size="16" type="text" style="width: 300px;">
    </form>
</div>

【问题讨论】:

  • 格式不正确的代码可读性要差得多,这可能会让人们看到你的问题(对我自己来说这绝对是真的)。您可以使用jsbeautifier.org 轻松格式化您的代码。
  • 控制台 (F12) 中是否有任何错误消息?啊,您尝试使用断点调试代码吗?

标签: php jquery jquery-ui jquery-autocomplete


【解决方案1】:

解决方案

将成功函数替换为以下内容:

success: function (data) {
    response($.map(data.geonames, function (item) {
        var text = item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName;
        return {
            label: text,
            value: text
        }
    }));
}

我猜"Selected: " 部分仅用于调试目的,所以基本上您要做的是将labelvalue 设置为相同的字符串。

说明

val() 的调用非常有效。但是您看不到预期的结果,因为该值被您的response 回调函数中设置的value 字段覆盖,这是autocomplete 的默认行为。

此外,select 事件永远不应该在 ui.item 未定义的情况下被调用,因为它会精确地对项目的选择做出反应......所以无论如何都不应该达到 "Nothing selected, input was " + this.value); 替代方案。

【讨论】:

    【解决方案2】:

    试试

    $("#appendedInputButton").attr('value',message);
    

    【讨论】:

    • 不,我们不要。首先,您希望设置value 属性,而不是value 属性,因此至少您应该使用.prop()。但是,.val() 方法是明确设计的,专门用于设置元素的值,为什么你会认为函数的选择是问题?
    • JQuery val() 函数用于执行此操作,就像 OP 使用它一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-04
    相关资源
    最近更新 更多