使用jquery从属性获取值和标签

时间:2013-10-29 08:51:05

标签: javascript jquery jsp

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

<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;
}

1 个答案:

答案 0 :(得分:1)

尝试

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

演示:Fiddle

使用.attr()

var moniker =  $(a).attr("data-autocomplete");
var json = JSON.parse(moniker);
console.log(json.value);