1

我正在使用 Spring MVC 进行自动完成(jquery)。我已经完成了一切,数据在自动完成中正确显示,但属性不显示 onfocus 事件。每当我在 onfocus 方法中调用“ui.item.username”时,它总是显示空值。

$( "#city" ).autocomplete({
    minLength: 0,
    source: function( request, response ) {
        $.ajax({
            url: "person.ajax",
            dataType: "json",
            data: {
                maxRows: 6,
                startsWith: request.term
            },
            success: function( data ) {
                response( $.map( data.zipcodes, function( item) {
                    return {
                        label: item.realName + item.realName,
                        value: item.username
                    }
                }));
            }
        });
    },

直到这里它工作正常但是当我在 onfocus 方法中调用属性(在以下方法中)时,它在焦点事件(jquery)中显示我为 null

focus: function(event, ui) {
    alert($( event.target ).val(ui.item.ealName)); // it displays me null value at this point
},
select: function( event, ui ) {
}

有什么建议吗?

4

2 回答 2

1

您在 AJAX 请求的函数中构建的结果对象success用于自动完成小部件的所有方法/事件处理程序。如果您想稍后访问某个属性,则必须在构建传递给response函数的数据源时包含该属性:

success: function( data ) {
    response( $.map( data.zipcodes, function( item) {
        return {
            label: item.realName + item.realName,
            value: item.username,
            realName: item.realName // include realName
        }
    }));
}

(来自评论):

alert函数也确实return null,因此如果您只想提醒该值,请使用:

alert(ui.item.realName)

反而。

于 2013-01-04T17:54:29.233 回答
0

我已经使用 spring mvc 实现了自动完成下拉。现在我的问题是,当任何用户从自动完成下拉菜单中选择/选择任何值时,我如何确定所选值是否正确?换句话说,我想确保用户是否从自动完成中选择了正确的值。因为用户可以在输入文本框中输入随机字符串并提交(从技术上讲,我还确保当用户从下拉列表中选择任何值时,提交将对用户可见,如果用户输入不正确的数据,按钮不应该被激活)。

这是一个代码

<script>
    $(function() {
        $( "input[name='creditCheck']" ).autocomplete({
            minLength: 2,
            source:function( request, response ) {
                $.ajax({
                    url: "creditCheck.ajax",
                    dataType: "json",
                    data: {
                        maxRows: 6,
                        startsWith: request.term
                    },
                success: function( data ) {
                    response( $.map( data.creditCheckData, function( item) {
                        return {
                            creditName: item.creditName,
                        }
                    }));
                }

                });
            },

上面的代码很好,它从服务器获取列表并显示自动完成数据。在下面的代码中,我只是切换按钮。但我也确保当用户从下拉列表中选择任何值时,提交将对用户,如果用户输入的数据不正确,则不应激活该按钮。

$('input[name="creditCheck"]').bind("change keyup", function () {
    if ($(this).val()!="") {
        $(this).nextAll("button[name='add']:first").removeAttr('disabled');
    } else {
        $(this).nextAll("button[name='add']:first").attr('disabled', 'disabled');
    }
});
于 2013-01-07T21:30:59.110 回答