我使用这段代码来设置一些asp.net标签值。每当标签值发生变化时,它们都会被更新。有25个标签,这段代码非常重复,对我来说效率很低。是否有一种方法可以在所有25个标签上循环,并在jQuery或标准javascript中设置Q#_Score_Possible
和Q#_Score variables
?
let Q1_Score_Possible;
let Q1_Score;
Q1_Score_Possible = $('#MainContent_grdScoring_lblPoints_Possible_0').val();
$('#MainContent_grdScoring_hiddenPoints_Possible_0').val(Q1_Score_Possible); //hidden field for database scoring
Q1_Score = $('#MainContent_grdScoring_lblScore_0').val();
$('#MainContent_grdScoring_hiddenScore_0').val(Q1_Score); //hidden field for database scoring
let Q2_Score_Possible;
let Q2_Score;
Q2_Score_Possible = $('#MainContent_grdScoring_lblPoints_Possible_1').val();
$('#MainContent_grdScoring_hiddenPoints_Possible_1').val(Q2_Score_Possible); //hidden field for database scoring
Q2_Score = $('#MainContent_grdScoring_lblScore_1').val();
$('#MainContent_grdScoring_hiddenScore_1').val(Q2_Score); //hidden field for database scoring
let Q3_Score_Possible;
let Q3_Score;
Q3_Score_Possible = $('#MainContent_grdScoring_lblPoints_Possible_2').val();
$('#MainContent_grdScoring_hiddenPoints_Possible_2').val(Q3_Score_Possible); //hidden field for database scoring
Q3_Score = $('#MainContent_grdScoring_lblScore_2').val();
$('#MainContent_grdScoring_hiddenScore_2').val(Q3_Score); //hidden field for database scoring
考虑以下内容:
$("input[id^='MainContent_grdScoring_lblPoints_Possible_']").each(function(i, el){
$("#MainContent_grdScoring_hiddenPoints_Possible_" + i).val($(el).val())
});
循环遍历选择器中的每个元素。查看更多:https://api.jquery.com/each/
通过索引,我们可以找到相应的隐藏元素。