1

我在表单中使用下面的脚本。我需要通过国家名称从 xml 文档中获取内容,然后将 genameId 放入隐藏文件中。自动完成 seacrh 工作正常,问题是如何将 geonameId 放入隐藏字段?

javascript:

<script type='text/javascript'> 
$(window).load(function(){

$.ajax({
 type: "GET",
 url: "Region.xml", // change to full path of file on server
 dataType: "xml",

success: function(xmlResponse) {
        var data = $("geoname", xmlResponse).map(function() {
            return {
                value: $("Name", this).text() ,
                id: $("geonameId", this).text()
            };
        }).get();
        $("#test").autocomplete({
            source: function(req, response) {
                var re = $.ui.autocomplete.escapeRegex(req.term);
                var matcher = new RegExp("^" + re, "i");
                response($.grep(data, function(item) {
                    return matcher.test(item.value);
                }));
            },
            minLength: 2,
            select: function(event, ui) {
                $("#result").html(ui.item ? ui.item.id : "Nothing selected, input was " + this.value);
            }
        });
    }
});
}); 

</script>

html:

<div class="ui-widget">

    <input id="test" />
</div>

<input type="hidden" name="result" value="">

如何将结果放入隐藏字段?

4

1 回答 1

1
        $("#test").autocomplete({
            source: function(req, response) {
                var re = $.ui.autocomplete.escapeRegex(req.term);
                var matcher = new RegExp("^" + re, "i");
                response($.grep(data, function(item) {
                    return matcher.test(item.value);
                }));
            },
            minLength: 2,
            select: function(event, ui) {
                $("input[name='result']").val(ui.item ? ui.item.id : "Nothing selected, input was " + this.value);
            }
        });
于 2013-10-02T08:03:08.330 回答