简体   繁体   English

使用jQuery定位数据元素内的对象

[英]Targeting object within a data element with jQuery

I'm trying to capture a value from an object in a data attribute. 我正在尝试从数据属性中的对象捕获值。 The example below is a shortened version of the element I'm working with. 下面的示例是我正在使用的元素的简化版本。

<div class="interest-select" data-ixp-input-state={
"instanceId":"iMABIId8pUGkQNMZ4izOLg",
"dataField":{\"id\":694,\"name\":\"ProductInterest\"}",
"validation":"valid",
"required":true,"value":"career",
"hasValue":true"}</div>

So far I've tried a few variations of the jQuery below, however this keeps returning undefined: 到目前为止,我已经尝试了以下jQuery的一些变体,但是这总是返回undefined:

var stateObj= $(".interest-select").attr("data-ixp-input-state")
var value= stateObj[5]//this should be equal to "career"

In fact the data-ixp-input-state value is a JSON string you should wrap it between two " first, then escape the " characters inside it or replace them with ' . 事实上, data-ixp-input-state值是一个JSON字符串,你应该把它包两者之间"然后再逃脱"字里面或替换它们'

Then you can get it's value using $(".interest-select").data("ixp-input-state") , you will get a string, you can then parse it to read intenal objects. 然后,您可以使用$(".interest-select").data("ixp-input-state")获得其值,您将获得一个字符串,然后可以对其进行解析以读取内部对象。

Demo: 演示:

This is a Demo: 这是一个演示:

 var value = $(".interest-select").data("ixp-input-state"); console.log(value); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="interest-select" data-ixp-input-state="{ 'instanceId':'iMABIId8pUGkQNMZ4izOLg', 'dataField':{'id':694,'name':'ProductInterest'}, 'validation':'valid', 'required':true,'value':'career', 'hasValue':true}"> the content</div> 

You have several JSON-syntax errors in that data attribute: such as dangling double quotes (after the last true , and after ProductInterest"} , ...). On the other hand, the whole data property's value should be quoted, and this is better done with another type of quote (single quote), so you don't have to do any escaping. 您在该data属性中存在几个JSON语法错误:例如,双引号悬挂(在最后一个true ,在ProductInterest"} ,...)。另一方面,应引用整个data属性的值,而这最好使用另一种类型的引号(单引号)来完成,因此您不必进行任何转义。

Also the div opening tag is not properly ended with a > . 此外, div开头标签未正确以>结尾。

If you correct all that, jQuery will translate that data attribute's value to a JS object when accessing it with the data method, using camel case: 如果您对此进行了更正,则当使用data方法使用驼峰式大小写访问时,jQuery会将该数据属性的值转换为JS对象:

 var stateObj= $(".interest-select").data("ixpInputState") console.log(stateObj); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="interest-select" data-ixp-input-state='{ "instanceId":"iMABIId8pUGkQNMZ4izOLg", "dataField":{"id":694,"name":"ProductInterest"}, "validation":"valid", "required":true,"value":"career", "hasValue":true}'></div> 

You had some syntax errors in your JSON: 您的JSON中有一些语法错误:

  • The 'data-' attribute should start and end with single quotations (to keep using the double quotes) “ data-”属性应以单引号开头和结尾(以继续使用双引号)
  • The dataField values shouldn't have the slash before the quotation marks dataField值不应在引号前使用斜杠
  • You had an extra quotation mark after the last true boolean 最后一个布尔值之后,您有一个额外的引号
  • There was a missing > at the end of the div div末尾缺少一个>

Small suggestion: indentation helps in these things to make the code more clear and easier to find issues 小建议:缩进有助于这些事情,使代码更清晰,更容易发现问题

<div class="interest-select"
    data-ixp-input-state='{
        "instanceId":"iMABIId8pUGkQNMZ4izOLg",
        "dataField": { "id": 694, "name": "ProductInterest" },
        "validation":"valid",
        "required":true,
        "value":"career",
        "hasValue":true
    }'></div>

And instead of the '.attr' method, you should use the '.data' method, the difference being that the latter will automatically parse the data into JSON format. 而不是使用'.attr'方法,您应该使用'.data'方法,不同之处在于后者将自动将数据解析为JSON格式。 You can then call each JSON value by adding '. 然后,您可以通过添加'来调用每个JSON值。 keyname ' after the object, like this: 对象后的keyname ',如下所示:

<script>
    var stateObj = $(".interest-select").data("ixp-input-state");
    var result = stateObj.value;
</script>

or this: 或这个:

<script>
    var result = $(".interest-select").data("ixp-input-state").value;
</script>

 var stateObj = $(".interest-select").attr("data-ixp-input-state"); stateObj = JSON.parse(stateObj); console.log(stateObj["value"]); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="interest-select" data-ixp-input-state='{ "instanceId":"iMABIId8pUGkQNMZ4izOLg", "dataField": { "id": 694, "name": "ProductInterest" }, "validation":"valid", "required":true, "value":"career", "hasValue":true }'></div> 

You should Parse your data. 您应该解析您的数据。

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

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