在输入中显示日期和时间

问题描述 投票:0回答:3

我目前有以下代码。我在HTML中添加了一个脚本,用于在采访开始时间输入中显示时间和日期(采访结束时间尚未添加。由于某种原因,这不起作用,只显示自动删除之前的瞬间。

我尝试更改脚本的位置,但这不起作用。我如何让开始时间留在那里,并在提交时输出?

const idToBold = [ 'start', 'name', 'profile', 'application', 'age', 'dob', 'origin', 'language' ];
var formInfo = {};

function showInput() {
  $('input').each(function(){
      var input = $(this);
      
      //here you check every <input type="text">
      if(input.attr('type') == 'text'){
        var value = input.val();
        
        //check if the id is in the constant of ids that need to add the [B] tag
        if(idToBold.includes(input.attr('id'))){
          value = '[b]' + value + '[/b]';
        }
        var label = $("label[for='"+input.attr('id')+"']").text();
        formInfo[label] = value;
      }
	  
	  //Age Check
      if(input.attr('name') == 'ageCondition' && input.is(':checked')){
        var message = null;
      
        //check the value, theres: 'pass' and 'fail'.
        if(input.val() == 'Yes'){
          message = '[b][Color = Blue]Match[/color][/b]';
        }else{
          message = '[b][Color = yellow]Age and Date of Birth do not match[/color][/b]';
        };
        var label = $("label[for='"+input.attr('name')+"']").text();
        formInfo[label] = message;
      }
      
      //Passed Interview
      if(input.attr('name') == 'passCondition' && input.is(':checked')){
        var message = null;
      
        //check the value, theres: 'pass' and 'fail'.
        if(input.val() == 'pass'){
          message = '[b][Color = Green]User has passed the interview [/color][/b]';
        }else{
          message = '[Color = Red]User hase failed the interview.[/color][/b]';
        };
        var label = $("label[for='"+input.attr('name')+"']").text();
        formInfo[label] = message;
      }
      
  });
  
  //you can remove this, just for output purpose
  var formInfoFormated = '';
  jQuery.each(formInfo, function(key, value){
    formInfoFormated += key + ': ' + value + '<br>';
  });
  $('#display').html(formInfoFormated);
}
*, *:before, *:after {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

body {
  font-family: 'Nunito', sans-serif;
  color: #384047;
}

form {
  max-width: 300px;
  margin: 10px auto;
  padding: 10px 20px;
  background: #f4f7f8;
  border-radius: 8px;
}


h1 {
  margin: 0 0 30px 0;
  text-align: center;
}

input[type="text"],
input[type="password"],
input[type="date"],
input[type="datetime"],
input[type="email"],
input[type="number"],
input[type="search"],
input[type="tel"],
input[type="time"],
input[type="url"],
textarea,
select {
  background: rgba(255,255,255,0.1);
  border: none;
  font-size: 16px;
  height: auto;
  margin: 0;
  outline: 0;
  padding: 15px;
  width: 100%;
  background-color: #e8eeef;
  color: #8a97a0;
  box-shadow: 0 1px 0 rgba(0,0,0,0.03) inset;
  margin-bottom: 30px;
}

input[type="radio"],
input[type="checkbox"] {
  margin: 0 4px 8px 0;
}

select {
  padding: 6px;
  height: 32px;
  border-radius: 2px;
}

.input_submit {
  padding: 19px 39px 18px 39px;
  color: #FFF;
  background-color: #4bc970;
  font-size: 18px;
  text-align: center;
  font-style: normal;
  border-radius: 5px;
  width: 100%;
  border: 1px solid #3ac162;
  border-width: 1px 1px 3px;
  box-shadow: 0 -1px 0 rgba(255,255,255,0.1) inset;
  margin-bottom: 10px;
}

fieldset {
  margin-bottom: 30px;
  border: none;
}

legend {
  font-size: 1.4em;
  margin-bottom: 10px;
}

label {
  display: block;
  margin-bottom: 8px;
}

label.light {
  font-weight: 300;
  display: inline;
}

.number {
  background-color: #5fcf80;
  color: #fff;
  height: 30px;
  width: 30px;
  display: inline-block;
  font-size: 0.8em;
  margin-right: 4px;
  line-height: 30px;
  text-align: center;
  text-shadow: 0 1px 0 rgba(255,255,255,0.2);
  border-radius: 100%;
}

@media screen and (min-width: 480px) {

  form {
    max-width: 480px;
  }

}
<html>
<head>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
	<script src ="javascript/supportJS.js"></script>
	<title>Arma Life - Interview Logger</title>
	<link rel="stylesheet" type="text/css" href="css/style.css">
	
	 <script>
	   var time = new Date();
	   function show(id) {
		 if (id == 1) {
			document.getElementById('start').value=time;
		 }

		 if(id == 2) {
			document.getElementById('end').value=time;
		 }
	   }
	</script>


</head>
<body>
    <form>
		<label for="start"><b>Interview Start Time</b></label>
		<input type="text" name="message" id="start">
		<button id='1' onClick="show(this.id)">Click Amber</button>
		<label for="name"><b>Roleplay Name</b></label>
		<input type="text" name="message" id="name">
		<label for="profile"><b>Profile Link</b></label>
		<input type="text" name="message" id="profile">
		<label for="application"><b>Application Link</b></label>
		<input type="text" name="message" id="application">
		
		<br><br>
		<label for="age"><b>Age</b></label>
		<input type="text" name="message" id="age">
		<label for="dob"><b>Date of Birth</b></label>
		<input type="text" name="message" id="dob">
		<label for="ageCondition"><b>Date of Birth and Age match?</b></label><br>
		<input type="radio" name="ageCondition" value="Yes">Yes<br>
		<input type="radio" name="ageCondition" value="No">No<br>
		<br><br>
		
		<label for="origin"><b>Country of Origin</b></label>
		<input type="text" name="message" id="origin">
		<label for="language"><b>Primary Language</b></label>
		<input type="text" name="message" id="language">
		
		<br><br>
		<label for="passCondition"><b>Passed?</b></label><br>
		<input type="radio" name="passCondition" value="pass">Pass<br>
		<input type="radio" name="passCondition" value="fail">Fail<br>
		<br><br>
	  </form>
	  
	  <input class="input_submit" type="submit" onclick="showInput();">

	  <label>Your input: </label>
	  <p><span id='display'></span></p>
</body>
</html>
javascript html
3个回答
2
投票

在HTML中,button元素默认为submit类型。

如果单击此按钮,则立即提交表单:

<button id='1' onClick="show(this.id)">Click Amber</button>

要使此按钮仅执行在click上定义的操作,而不提交表单,只需给它一种button

<button id='1' type="button" onClick="show(this.id)">Click Amber</button>

const idToBold = [ 'start', 'name', 'profile', 'application', 'age', 'dob', 'origin', 'language' ];
var formInfo = {};


function showInput() {
  $('input').each(function(){
      var input = $(this);
      
      //here you check every <input type="text">
      if(input.attr('type') == 'text'){
        var value = input.val();
        
        //check if the id is in the constant of ids that need to add the [B] tag
        if(idToBold.includes(input.attr('id'))){
          value = '[b]' + value + '[/b]';
        }
        var label = $("label[for='"+input.attr('id')+"']").text();
        formInfo[label] = value;
      }
	  
	  //Age Check
      if(input.attr('name') == 'ageCondition' && input.is(':checked')){
        var message = null;
      
        //check the value, theres: 'pass' and 'fail'.
        if(input.val() == 'Yes'){
          message = '[b][Color = Blue]Match[/color][/b]';
        }else{
          message = '[b][Color = yellow]Age and Date of Birth do not match[/color][/b]';
        };
        var label = $("label[for='"+input.attr('name')+"']").text();
        formInfo[label] = message;
      }
      
      //Passed Interview
      if(input.attr('name') == 'passCondition' && input.is(':checked')){
        var message = null;
      
        //check the value, theres: 'pass' and 'fail'.
        if(input.val() == 'pass'){
          message = '[b][Color = Green]User has passed the interview [/color][/b]';
        }else{
          message = '[Color = Red]User hase failed the interview.[/color][/b]';
        };
        var label = $("label[for='"+input.attr('name')+"']").text();
        formInfo[label] = message;
      }
      
  });
  
  //you can remove this, just for output purpose
  var formInfoFormated = '';
  jQuery.each(formInfo, function(key, value){
    formInfoFormated += key + ': ' + value + '<br>';
  });
  $('#display').html(formInfoFormated);
}
*, *:before, *:after {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

body {
  font-family: 'Nunito', sans-serif;
  color: #384047;
}

form {
  max-width: 300px;
  margin: 10px auto;
  padding: 10px 20px;
  background: #f4f7f8;
  border-radius: 8px;
}


h1 {
  margin: 0 0 30px 0;
  text-align: center;
}

input[type="text"],
input[type="password"],
input[type="date"],
input[type="datetime"],
input[type="email"],
input[type="number"],
input[type="search"],
input[type="tel"],
input[type="time"],
input[type="url"],
textarea,
select {
  background: rgba(255,255,255,0.1);
  border: none;
  font-size: 16px;
  height: auto;
  margin: 0;
  outline: 0;
  padding: 15px;
  width: 100%;
  background-color: #e8eeef;
  color: #8a97a0;
  box-shadow: 0 1px 0 rgba(0,0,0,0.03) inset;
  margin-bottom: 30px;
}

input[type="radio"],
input[type="checkbox"] {
  margin: 0 4px 8px 0;
}

select {
  padding: 6px;
  height: 32px;
  border-radius: 2px;
}

.input_submit {
  padding: 19px 39px 18px 39px;
  color: #FFF;
  background-color: #4bc970;
  font-size: 18px;
  text-align: center;
  font-style: normal;
  border-radius: 5px;
  width: 100%;
  border: 1px solid #3ac162;
  border-width: 1px 1px 3px;
  box-shadow: 0 -1px 0 rgba(255,255,255,0.1) inset;
  margin-bottom: 10px;
}

fieldset {
  margin-bottom: 30px;
  border: none;
}

legend {
  font-size: 1.4em;
  margin-bottom: 10px;
}

label {
  display: block;
  margin-bottom: 8px;
}

label.light {
  font-weight: 300;
  display: inline;
}

.number {
  background-color: #5fcf80;
  color: #fff;
  height: 30px;
  width: 30px;
  display: inline-block;
  font-size: 0.8em;
  margin-right: 4px;
  line-height: 30px;
  text-align: center;
  text-shadow: 0 1px 0 rgba(255,255,255,0.2);
  border-radius: 100%;
}

@media screen and (min-width: 480px) {

  form {
    max-width: 480px;
  }

}
<html>
<head>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
	<script src ="javascript/supportJS.js"></script>
	<title>Arma Life - Interview Logger</title>
	<link rel="stylesheet" type="text/css" href="css/style.css">
	
	 <script>
	   var time = new Date();
	   function show(id) {
		 if (id == 1) {
			document.getElementById('start').value=time;
		 }

		 if(id == 2) {
			document.getElementById('end').value=time;
		 }
	   }
	</script>


</head>
<body>

	  <form>
		<label for="start"><b>Interview Start Time</b></label>
		<input type="text" name="message" id="start">
		<button id='1' type="button" onClick="show(this.id)">Click Amber</button>
		<label for="name"><b>Roleplay Name</b></label>
		<input type="text" name="message" id="name">
		<label for="profile"><b>Profile Link</b></label>
		<input type="text" name="message" id="profile">
		<label for="application"><b>Application Link</b></label>
		<input type="text" name="message" id="application">
		
		<br><br>
		<label for="age"><b>Age</b></label>
		<input type="text" name="message" id="age">
		<label for="dob"><b>Date of Birth</b></label>
		<input type="text" name="message" id="dob">
		<label for="ageCondition"><b>Date of Birth and Age match?</b></label><br>
		<input type="radio" name="ageCondition" value="Yes">Yes<br>
		<input type="radio" name="ageCondition" value="No">No<br>
		<br><br>
		
		<label for="origin"><b>Country of Origin</b></label>
		<input type="text" name="message" id="origin">
		<label for="language"><b>Primary Language</b></label>
		<input type="text" name="message" id="language">
		
		<br><br>
		<label for="passCondition"><b>Passed?</b></label><br>
		<input type="radio" name="passCondition" value="pass">Pass<br>
		<input type="radio" name="passCondition" value="fail">Fail<br>
		<br><br>
	  </form>
	  
	  <input class="input_submit" type="submit" onclick="showInput();">

	  <label>Your input: </label>
	  <p><span id='display'></span></p>
</body>
</html>

1
投票

好的,为了防止您的内容消失,请删除表单标记。从我看到你必须形成元素存在,但你没有使用它的预期目的。当您单击“单击琥珀色”按钮时,表单标签正在尝试执行POST(这就是为什么所有内容都会消失)。表单认为您的按钮是在那里做“提交”。

以下是有关html表单元素的更多信息:https://www.w3schools.com/html/html_forms.asp


1
投票

您可以将type="button"添加到“单击琥珀色”按钮,以防止您的表单在该印刷机上提交。如前所述,您的表单正在提交,正在清理您的页面。

您的输出代码已正常工作。

<button id='1' type="button" onClick="show(this.id)">Click Amber</button>
© www.soinside.com 2019 - 2024. All rights reserved.