简体   繁体   English

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

[英]getting value and label from an attribute using jquery

From the following a I need to get value from the data-autocomplete attribute (we have label and value in there but I need to collect only value) by using jQuery, please help. 从下面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>

This is code how I am trying : 这是我正在尝试的代码:

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

Try 尝试

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

Demo: Fiddle 演示: 小提琴

Using .attr() 使用.attr()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM