gpt4 book ai didi

jquery - 捕获数据属性

转载 作者:行者123 更新时间:2023-12-01 08:21:33 25 4
gpt4 key购买 nike

在迭代嵌套在组和节中的一组输入时,我想 try catch 数据组和节组,但需要推送...

<script src="jquery.mobile/jquery.js"></script>

<div id="groupA" data-group="A" class="preGroups">

<div id="section-A1" data-section="1">
<input name="SRPR1" type="text">
<input name="SRPR2" type="text">
</div>

<div id="section-A2" data-section="2">
<input name="SRPR1" type="text">
<input name="SRPR2" type="text">
</div>

<div id="section-A3" data-section="3">
<input name="SRPR1" type="text">
<input name="SRPR2" type="text">
</div>

<div id="section-A4" data-section="4">
<input name="SRPR1" type="text">
<input name="SRPR2" type="text">
</div>
</div>

<div id="groupB" data-group="B" class="preGroups">

<div id="section-B1" data-section="1">
<input name="SRPR1" type="text" value="1">
<input name="SRPR2" type="text"value="1">
</div>

<div id="section-B2" data-section="2">
<input name="SRPR1" type="text"value="1">
<input name="SRPR2" type="text">
</div>

<div id="section-B3" data-section="3">
<input name="SRPR1" type="text">
<input name="SRPR2" type="text">
</div>
</div>

<script>


var currentGroup = "";
var currentSection = "";

// iterate through each group in groups
groups = $('div[id^="group"]');
$.each(groups, function(key, group) {
currentGroup = group.getAttribute('group');
// iterate through each section in group
sections = $(group).find('div[id^="section"]');
$.each(sections, function(key, section) {
currentSection = section.getAttribute('section');

var inputs = $(section).find("input");
inputs.each(function(){

fnValidateDetails(currentGroup, currentSection, this.name, this.value)
})
});
});

function fnValidateDetails(currentGroup, currentSection, theName, theValue ){

console.log(currentGroup, currentSection, theName, theValue);

}

</script>

最佳答案

您可以使用 jQuery 通过以下两种方式之一访问数据属性:

$('div').data('group');
$('div').attr('data-group');

$('div').data('section');
$('div').attr('data-section');

不确定这对您来说是否足够。对于“捕获”的含义有点模糊。

关于jquery - 捕获数据属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7084072/

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