使用jQuery-UI自动完成选择选项时设置多个输入的值

时间:2015-10-06 12:45:32

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

大家好我是java-script的新手。所以我希望你能帮助我。当用户将数据设置到城市字段时,我想自动在国家/地区字段中设置数据。 我有一个xml文件:

<ROWSET>
<ROW>
<city></city>
<country></country>
</ROW>
</ROWSET>

在html文档中,我有两个输入字段

<input id="id_city">
<input id="country">

这是我的js代码:

$.ajax({
    url: "/media/xml/country.xml", //your url
    type: "GET", //may not need this-fiddle did-default is GET
    dataType: "xml",
    success: function(xmlResponse) {
        var data = $("ROW", xmlResponse).map(function() {
            return {
                value: $("city", this).text() + ", " + ($.trim($("country", this).text()) || "(unknown country)")
            };
        }).get();
        $("#id_city").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) {
                $("p#result").html(ui.item ? "Selected: " + ui.item.value + ", cityId: " + ui.item.id : "Nothing selected, input was " + this.value);
            }
        });
    }
});

我不知道如何自动将数据设置为国家/地区字段 感谢。

1 个答案:

答案 0 :(得分:1)

好的,这应该为你做。我已经更改了UIImagePickerController的定义,所以它现在有三个属性:

  • @IBAction func takePhoto(sender: UIButton) { imagePicker = UIImagePickerController() imagePicker?.allowsEditing = true imagePicker?.delegate = self imagePicker?.sourceType = .Camera presentViewController(imagePicker!, animated: true, completion: nil) } func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { let imageURL = info[UIImagePickerControllerReferenceURL] as? NSURL print(" Path \(imageURL)") imageView?.contentMode = .ScaleAspectFit imageView?.image = info[UIImagePickerControllerOriginalImage] as? UIImage imagePicker?.dismissViewControllerAnimated(true, completion: nil) } 与原始data相同。
  • label您可以将其放入value输入中。
  • city因此您可以将其放入#city输入中。

这意味着您可以在自动填充的select属性中使用country#country

因为现在有三个属性需要自定义自动填充,并告诉它使用ui.item.city作为_renderItem部分的选项。

ui.item.country

此外,我创建了一个“工作”Fiddle(因为您的代码使用的是ajax XML源我已经在变量中硬编码了响应,并在label属性中生成了自动完成功能ajax请求总是会从jsfiddle.net失败。