gpt4 book ai didi

javascript - 将对象值动态添加到

标签内的 div

转载 作者:可可西里 更新时间:2023-11-01 13:27:03 26 4
gpt4 key购买 nike

单击按钮后,我想将 p 标记添加到现有的 div 中,其中包含对象属性名称的值。

  var foodLoads = {
orange: 6.7,
apple: 5.6,
banana: 12.7,
grapes: 16.3,
peach: 2.7,
pear: 6.5,
mango: 16.1,
blueberries: 9.3,
grapefruit: 1.7,
strawberry: 3.5,
tangerine: 3.1,
watermelon: 8,
duck: 0,
beef: 0,
chicken: 0,
ham: 0,
turkey: 0,
elk: 0,
pork: 0,
fish: 0,
eggs: 0,
lamb: 0,
applejuice: 11.8,
cranberryjuice: 23.3,
orangejuice: 14.4,
carrotjuice: 8.6,
lemonade: 24.3,
hotchocolatemix: 10.2,
tomatojuice: 3.5,
chocolatemilk: 13.3,
almondmilk: 0.02,
soymilk: 4,
wholewheatbread: 6.1,
whitebread: 10.7,
bagel: 30,
waffle: 13.8,
pancake: 5.3,
croissant: 12.2,
muffin: 28.8,
englishmuffin: 21.3,
doughnut: 15.2,
oatmeal: 12.6,
quinoa: 20.4,
wholegrainbread: 7.1
}

div 是在用户选中复选框时动态创建的(这是在他们单击按钮之前)。

  $('input').on('ifChecked', function(event) {       
var liText = $(this).parent().parent().text();
var wrappedUp = $('<div class="active"><li>' + liText + '</li><select class="serving-size"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select></div>');
$('.food-list').append(wrappedUp);
$(wrappedUp).addClass('' + liText + '');
})

这是我正在谈论的 div 的一个例子

<ul class="food-list">

<div class="active Mango">
<li>Mango</li>
<select class="serving-size"><option value="1">1</option><optionvalue="2">2</option><option value="3">3</option><option value="4">4</option>
</select>
</div>
</ul>

所以,基本上,在单击一个按钮时,我希望能够使用该类(即 Mango)将其与 foodLoads 对象中具有相同名称(mango)的属性相匹配,将该属性的值放在 p 标记中, 并将其添加到具有匹配类 (Mango) 的 div。

这是我尝试使用的 jQuery。由于我是 jQuery 的新手,所以我确信语法全都搞砸了。

  $('.submit-items').click(function() {
$.each( foodLoads, function( key, value ) {
if ($('.active:contains(' + key + ')')) {
$(this).append('<p>' + key + '</li>');
}
})

})

有什么想法吗?提前致谢:)

最佳答案

这里有一个更简单的方法。

  • active 类中找到 li 的文本。
  • 查找它的值。
  • 附加p

喜欢:-

$('.submit-items').click(function() {
var type = $('.food-list .active li').text();
var value = foodLoads[type.toLowerCase()];
$('.' + type).append('<p>' + value + '</p>');
});

演示

var foodLoads = {
orange: 6.7,
apple: 5.6,
banana: 12.7,
grapes: 16.3,
peach: 2.7,
pear: 6.5,
mango: 16.1,
blueberries: 9.3,
grapefruit: 1.7,
strawberry: 3.5,
tangerine: 3.1,
watermelon: 8,
duck: 0,
beef: 0,
chicken: 0,
ham: 0,
turkey: 0,
elk: 0,
pork: 0,
fish: 0,
eggs: 0,
lamb: 0,
applejuice: 11.8,
cranberryjuice: 23.3,
orangejuice: 14.4,
carrotjuice: 8.6,
lemonade: 24.3,
hotchocolatemix: 10.2,
tomatojuice: 3.5,
chocolatemilk: 13.3,
almondmilk: 0.02,
soymilk: 4,
wholewheatbread: 6.1,
whitebread: 10.7,
bagel: 30,
waffle: 13.8,
pancake: 5.3,
croissant: 12.2,
muffin: 28.8,
englishmuffin: 21.3,
doughnut: 15.2,
oatmeal: 12.6,
quinoa: 20.4,
wholegrainbread: 7.1
};

$('.submit-items').click(function() {
var type = $('.food-list .active li').text();
var value = foodLoads[type.toLowerCase()];
$('.' + type).append('<p>' + value + '</p>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul class="food-list">
<div class="active Mango">
<li>Mango</li>
<select class="serving-size">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
</ul>

<button class="submit-items">submit</button>

关于javascript - 将对象值动态添加到 <p> 标签内的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36148379/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com