jQuery UI自动完成不向script_url发送隐藏的输入新值

时间:2012-12-15 22:35:30

标签: jquery jquery-ui autocomplete jquery-autocomplete jquery-ui-autocomplete

使用jQuery UI自动完成设置隐藏输入值后

$( "#PName" ).autocomplete({            
            search: function(event, ui) {
             $("#PLoading").show();
            },
            source: "Script_URL",
            minLength: 2,            
            select: function( event, ui ) {
                $("#PLoading").hide();
                $("#PName").val(ui.item.value);
                $("#PID").val(ui.item.id);
            }
        });

<input type="hidden" id="PID" name="PID" value="0" />

用于通过自动完成功能将隐藏输入的新​​值发送到其他网址,发送0值

0值是隐藏输入默认值,(对于测试,我将默认值更改为90,但再次发送默认值(90))

 $("#CName").autocomplete({    
        search: function(event, ui) {
         $("#CLoading").show();
    alert ($("#PID").val()); // it's worked, and alert new value , from hidden input
        },
        source: "Script_URL/"+$("#PID").val(),  /// but it's not worked ,  does not send new value from hidden input 
        minLength: 2,            
        select: function( event, ui ) {
            $("#CLoading").hide();
            $( "#CName" ).val(ui.item.value);
            $( "#CID" ).val(ui.item.id);

        }
    });

1 个答案:

答案 0 :(得分:2)

当窗口小部件初始化时,您的代码只会读取隐藏输入的值一次。如果您想每次都更新源URL,则必须为source参数提供一个函数并自行发出AJAX请求:

$( "#PName" ).autocomplete({            
    search: function(event, ui) {
        $("#PLoading").show();
    },
    source: function () {
        $.ajax({
            url: "Script_URL/" + $("#PID").val(),
            data: request,
            dataType: "json",
            success: function (data) {
                response(data);
            },
            error: function () {
                response([]);
            }
        });
    },
    minLength: 2,            
    select: function( event, ui ) {
        $("#PLoading").hide();
        $("#PName").val(ui.item.value);
        $("#PID").val(ui.item.id);
    }
});