我的输入字段编码如下:
<input type="radio" name="03794" class=" js-sourcing-option" data-sourcingoption="{"hasBackOrder":true,"isPartial":false,"unsourcedQty":0,"warehouseSequences":[1],"shipments":{"2017-10-12T17:00:00":{"warehouseNumber":101,"deliverByTime":"2017-10-12T17:00:00","numShipments":0,"warehouseQty":0,"backorderQty":1,"qtyByWarehouseInStock":{},"qtyByWarehouseBackorder":{"101":1}}},"sourcedQty":1,"shouldDisplay":true}">
使用jQuery,我无法弄清楚如何将“deliverByTime”(应该等于“2017-10-12T17:00:00”)的值深深植入此字段的“data-sourcingoption”属性中。< / p>
使用以下输出“name”属性(下面有“[0]”,因为页面上有更多这些属性):
$('input.js-sourcing-option')[0]["name"]
// Output: 03794
但是,如果我为“数据采购选择”做类似的事情,尝试使用结果并提取我需要的东西,我得到了这个:
$('input.js-sourcing-option')[0]["data-sourcingoption"]
// Output: undefined
非常感谢任何帮助......谢谢!
答案 0 :(得分:0)
data-sourcingoption
- 无效的属性值。
使用json字符串添加隐藏的div,如果nided则获取.html()
值。
$('input:radio[name=03794]').change(function () {
var radio_val=$(this).val();
var json_str=$('#div_for_03794_'+radio_val).html();
var jsonobj = JSON.parse(json_str);
console.log(jsonobj);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="radio" name="03794" class=" js-sourcing-option" value="1">
<div id="div_for_03794_1" style="display:none;">
{"hasBackOrder":true,"isPartial":false,"unsourcedQty":0,"warehouseSequences":[1],"shipments":{"2017-10-12T17:00:00":{"warehouseNumber":101,"deliverByTime":"2017-10-12T17:00:00","numShipments":0,"warehouseQty":0,"backorderQty":1,"qtyByWarehouseInStock":{},"qtyByWarehouseBackorder":{"101":1}}},"sourcedQty":1,"shouldDisplay":true}
</div>
<input type="radio" name="03794" class=" js-sourcing-option" value="2">
<div id="div_for_03794_2" style="display:none;">
{"hasBackOrder":false,"isPartial":true,"unsourcedQty":0,"warehouseSequences":[1],"shipments":{"2017-10-12T17:00:00":{"warehouseNumber":101,"deliverByTime":"2017-10-12T17:00:00","numShipments":0,"warehouseQty":0,"backorderQty":1,"qtyByWarehouseInStock":{},"qtyByWarehouseBackorder":{"101":1}}},"sourcedQty":1,"shouldDisplay":true}
</div>
答案 1 :(得分:0)
数据属性语法似乎无效。
您可以通过以下方式将数据属性的外引号更改为单引号吗?
<input type="radio" name="03794" class=" js-sourcing-option" data-sourcingoption='{"hasBackOrder":true,"isPartial":false,"unsourcedQty":0,"warehouseSequences":[1],"shipments":{"2017-10-12T17:00:00":{"warehouseNumber":101,"deliverByTime":"2017-10-12T17:00:00","numShipments":0,"warehouseQty":0,"backorderQty":1,"qtyByWarehouseInStock":{},"qtyByWarehouseBackorder":{"101":1}}},"sourcedQty":1,"shouldDisplay":true}'>
要进一步回答你的问题,(即提取deliverByTime),你可以使用这个或你用来提取属性的方法:
console.log($('input.js-sourcing-option').data('sourcingoption').shipments['2017-10-12T17:00:00'].deliverByTime)
答案 2 :(得分:0)
dplyr
我正在使用javascript添加“data-sourcingoption”属性 console.log是deliverByTime
的输出答案 3 :(得分:0)
全部 -
我最终玩弄了这条路。它适用于可能的多个条目,等等。我认为它可以解决:
$('.js-sourcing-option').each(function() {
var dataSourcing = $(this).data('sourcingoption');
$.each(dataSourcing.shipments, function(date, shipment) {
console.log(shipment.warehouseNumber, new Date(shipment.deliverByTime));
});
});