0

从下面a我需要使用jQuery从属性中获取值data-autocomplete(我们在那里有标签和值,但我只需要收集值),请帮助。

<a href="nullAUS%7C6214051%7C0HOAUSAQjdBwAAAAAIAwEAAAAA2igAgAAAAAAAAABkAAAAAP....8AAAAAAAAAAABub3J0AA--" data-transition="fade" data-autocomplete="{&quot;label&quot;:&quot;North Chigwell Primary, 115 Allunga Road, CHIGWELL  TAS  7011&quot;,&quot;value&quot;:&quot;AUS|6214051|0HOAUSAQjdBwAAAAAIAwEAAAAA2igAgAAAAAAAAABkAAAAAP....8AAAAAAAAAAABub3J0AA--&quot;}" class="ui-link-inherit">North Chigwell Primary, 115 Allunga Road, CHIGWELL  TAS  7011</a>

这是我正在尝试的代码:

callback: function (e) {

    var a = $(e.currentTarget); // access the selected item
    var address = $.trim(a.text());
    // data-autocomplete

    var moniker = $(a).attr("data-autocomplete");

    $('#addressQas').val(address);
    //$.ajax({
    new FancyAjaxinator().jsonPost({
        url: "/address/selectLine",
        //dataType: "json",
        data: {
            "moniker": moniker,
                "address": address
        },
        success: function (data) {
            //console.log(data.responseObject);
            var qasAddress = data.responseObject;
            $('#address-line1').val(qasAddress.addressLine1);
            if (qasAddress.state) {
                $('#address-line2').val(qasAddress.addressLine2);
                $('#address-townCity').val(qasAddress.suburb);
                $('#address-state').val(qasAddress.state);
                $('#address-state').parent().find('span.ui-selectmenu-text').text($("#address-state option[value=" + qasAddress.state + "]").text());
                $('#address-postcode').val(qasAddress.postcode);
                $('#address-line1').valid();
                $('#address-townCity').valid();
                $('#address-state').valid();
                $('#address-postcode').valid();
            }
            $('#QASaddressField').hide();
            $('.qasAddressFields').show();
            //setFocus();
        }
    });
    this.close;
    return false;
}
4

1 回答 1

1

尝试

var a =  $('a');// or the jQuery wrapper for the target a
var json =a.data('autocomplete');
console.log(json.value)

演示:小提琴

使用 .attr()

var moniker =  $(a).attr("data-autocomplete");
var json = JSON.parse(moniker);
console.log(json.value);
于 2013-10-29T08:57:31.340 回答