将输入转换为 string/var 并在警报中使用它

Convert input into string/var and use it in an alert

我正在使用输入并希望将其转换为 string/var,以便在我单击按钮后在警报中将其打印出来。

我需要 "Forename:" 的输入以使用 "onlcick" 添加到按钮 "send"。

警报只是一个调试器。我想使用一个函数而不是警报。使用 onclick="printPDF(foreName+' '' ' + da + '.' + mo + '.' + ye + '_' + h + ':' + m + ':' + s + '.pdf');

function startTime() {
  var today = new Date();
  var da = today.getDate();
  var mo = today.getMonth() + 1;
  var ye = today.getFullYear();
  var h = today.getHours();
  var m = today.getMinutes();
  var s = today.getSeconds();
  m = checkTime(m);
  s = checkTime(s);
}

function checkTime(i) {
  if (i < 10) {
    i = "0" + i
  }; // add zero in front of numbers < 10
  return i;
}
<p>
  <label>Forename:</label>
  <input class="w3-input w3-white" id="some1" type="text" name="some1" value="asd" maxLength="200">
</p>

<button id="snbtn1" type="submit" class="w3-btn w3-green" onclick="alert(' ' + da + '.' + mo + '.' + ye + '_' + h + ':' + m + ':' + s + '.pdf');">send</button>

您应该将 onclick 代码放入 javascript 代码的函数中,方法是获取按钮元素并向其添加事件侦听器。以与在 javascript 代码中获取按钮相同的方式,您可以获得输入及其值:

var today = new Date();
var da = today.getDate();
var mo = today.getMonth()+1;
var ye = today.getFullYear();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
//m = checkTime(m);
//s = checkTime(s);

document.getElementById("snbtn1").addEventListener("click", function() { // Get the button and add an onclick event listener to it
  alert(' ' + da + '.' + mo + '.' + ye + '_' + h + ':' + m + ':' + s + '.pdf');
  alert(document.getElementById("some1").value); // Get the input by it's id and alert the value
});
<p>
<label>Forename:</label>
<input class="w3-input w3-white" id="some1" type="text" name="some1" value="asd" maxLength="200">
</p>

<button id="snbtn1" type="submit" class="w3-btn w3-green">send</button>

https://developer.mozilla.org/he/docs/Web/API/Document/getElementById

https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

<script>
function startTime() {
      var today = new Date();
      var da = today.getDate();
      var mo = today.getMonth()+1;
      var ye = today.getFullYear();
      var h = today.getHours();
      var m = today.getMinutes();
      var s = today.getSeconds();
      m = checkTime(m);
      s = checkTime(s);
      var foreName = document.getElementById('some1').value;

      alert(foreName+' ' + da + '.' + mo + '.' + ye + '_' + h + ':' + m + ':' + s + '.pdf');
}

function checkTime(i) {
  if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
  return i;
}
</script>
<p>
    <label>Forename:</label>
    <input class="w3-input w3-white" id="some1" type="text" name="some1" value="asd" maxLength="200">
</p>

   <button id="snbtn1" type="submit" class="w3-btn w3-green"  onclick="startTime();">send</button>