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

时间:2018-03-01 15:57:59

标签: javascript jquery html forms

这是我的第一个问题,希望它的格式正确。 这个想法是在按下#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();
&#13;
<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>
&#13;
&#13;
&#13;

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

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

1 个答案:

答案 0 :(得分:1)

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

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