gpt4 book ai didi

javascript - 单击按钮后获取所有输入字段值。 ( JavaScript )

转载 作者:行者123 更新时间:2023-11-30 20:42:53 26 4
gpt4 key购买 nike

这是我的第一个问题,希望它的格式正确。这个想法是,在按下 #grabValues 按钮后,输入值将存储到一个数组中,用于计算总小时数。

我计划在外部 js 文件中编写该部分,因为我假设它需要加载到主体中,而另一个需要加载到头部。

感谢阅读。

console.log('Script has loaded!');
function formMaker(){
var dayCount;
var myMonth = document.getElementById('inputMonth').value;
console.log(myMonth);
if( myMonth.includes('jan', 'Jan', 'JAN')){
dayCount = 31;
} else if ( myMonth.includes('feb', 'Feb', 'FEB')){
dayCount = 28;
} else if ( myMonth.includes('mar', 'Mar', 'MAR')){
dayCount = 31;
} else if ( myMonth.includes('apr', 'Apr', 'APR')){
dayCount = 30;
} else if ( myMonth.includes('may', 'May', 'MAY')){
dayCount = 31;
} else if ( myMonth.includes('jun', 'Jun', 'JUN')){
dayCount = 30;
} else if ( myMonth.includes('jul', 'Jul', 'JUL')){
dayCount = 31;
} else if ( myMonth.includes('aug', 'Aug', 'AUG')){
dayCount = 31;
} else if ( myMonth.includes('sep', 'Sep', 'SEP')){
dayCount = 30;
} else if ( myMonth.includes('oct', 'Oct', 'OCT')){
dayCount = 31;
} else if ( myMonth.includes('nov', 'Nov', 'NOV')){
dayCount = 30;
} else if ( myMonth.includes('dec', 'Dec', 'DEC')){
dayCount = 31;
} else {
return;
}

for(var i=0; i < dayCount; i++){
// Loop this with dayCounter value.
var myFormGroup = document.getElementById('myFormGroup');

var myLabel = document.createElement('label');
myLabel.for = 'hours';
myLabel.innerHTML = 'Hours worked:'; // + day/month

var myInput = document.createElement('input');
myInput.type = "number";
// Add class of month for later.
myInput.className = 'form-control hours ' + myMonth.toString();
myInput.placeholder = 'amount of hours here:';

myFormGroup.appendChild(myLabel);
myFormGroup.appendChild(myInput);

//Add different id's to each inputfield being created. JQUERY!
$('#myFormGroup input').attr('id', function(i) {
return 'hour_inputs'+(i++);
})
console.log('Form(s) has/have been made, with different input IDs');
}
// End of loop


/*$("input").blur(function(){
alert("This input field has lost its focus.");

});
*/

console.log('TEST');
};
// End of formMaker();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
<meta charset="utf-8">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" type="number/css" href="workhours.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="formbuilder.js"></script>

<title>Work hours</title>

</head>
<body>
<div class="section_row_main">
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>Let's calculate the hours you've worked: month here</h2>

</div>
<div class="col-md-12">
<div class="form-group" id="myFormGroup_test">
<label for='month'>Month:</label>
<input type='text' class="form-control month" placeholder="enter month" id="inputMonth">
<button id="formsNow" onclick="formMaker();">Make forms</button>
</div>
<div class="form-group" id="myFormGroup">
<!-- -->
</div>
<button id="grabValues">grabValues!</button>
</div>
</div>
</div>
<script src="hourChecker.js"></script>

</body>
</html>

这当前返回单击按钮时第一个输入字段的值:

$('#grabValues').click(function() { 
$("#myFormGroup input[id^='hour_inputs'][type=number]").val();
console.log($("#myFormGroup input[id^='hour_inputs']").val());
});

最佳答案

您需要使用$.each 函数获取所有元素。试试这个代码。

$('#grabValues').click(function() {
$.each($("#myFormGroup input[id^='hour_inputs'][type=number]"), function( key, value ) {
console.log(this.value);
});
});

关于javascript - 单击按钮后获取所有输入字段值。 ( JavaScript ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49053697/

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