如何显示日期与文本框值

Jquery: How to show date with textbox value?

本文关键字:文本 日期 何显示 显示      更新时间:2023-09-26

我是jquery新手,我写了下面的代码

<h1>Type your comment below </h1>
<h2>TextBox value : <label id="msg"></label>-<label id="date"></label></h2>
<div style="padding:16px;">
    TextBox : <input type="text" value="" placeholder="Type Your Comment"></input>
</div>
<button id="Get">Get TextBox Value</button> 
var fullDate = new Date();
$("button").click(function(){
    $('#msg').html($('input:text').val());        
}); 

如何显示日期时提交按钮按下标签有id="date" ?当用户按下提交按钮时,我想显示

"User entered Textbox Value - Date with time"

只需使用适当的css选择器来添加消息和日期。参考下面的代码:

 $("button").click(function() {
    var msg = $('input:text').val();
    var fullDate = new Date();
    $('#msg').html(msg);
    $('#date').html(toLocal(fullDate));
  });
 function toJSONLocal (date) {
   var local = new Date(date);
   local.setMinutes(date.getMinutes() - date.getTimezoneOffset());
   return local.toJSON().slice(0, 10);
 }
 function toLocal (date) {
   var local = new Date(date);
   local.setMinutes(date.getMinutes() - date.getTimezoneOffset());
   return local.toJSON().replace('T', ' ').slice(0, 19);
 }

注意:toJSONLocal, toLocal用于格式化日期。

jsfiddle: https://jsfiddle.net/nikdtu/q8zmLebz/

就用这个:

var fullDate = new Date();
$('#date').text(fullDate);

$(function() {
  $("#Get").click(function() {
    var fullDate = new Date();
    $('#date').text(fullDate);
    $('#msg').text($('#comment').val());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<h1>Type your comment below </h1>
<h2>TextBox value : <label id="msg"></label>-<label id="date"></label></h2>
<div style="padding:16px;">
  TextBox :
  <input type="text" value="" placeholder="Type Your Comment" id="comment" />
</div>
<button id="Get">Get TextBox Value</button>