drhdjp97757
2018-02-10 04:43
浏览 98

如何在Jquery中按下每个按钮获取新的时间或日期?

When I enter multiple notes on button click Jquery date and time auto fill up in the textbox. Like this,

enter image description here

But when I click After two-minute date and time not change its display same as first selected.

Below the Code :

  1. Jquery Date and Time Code:

    var nowDate = new Date(); 
    var mm = nowDate.getMonth() + 1; mm = (mm < 10) ? '0' + mm : mm;
    var dd = nowDate.getDate(); dd = (dd < 10) ? '0' + dd : dd;
    var new_today = mm + '/' + dd + '/' + nowDate.getFullYear();
    
    var mi = nowDate.getMinutes(); mi = (mi < 10) ? '0' + mi : mi;
    var time = nowDate.getHours() + ":" + mi;
    
  2. Dynamic HTML Code

     function addCallImage() {
    
      html  = '<tr id="call-row' + call_row + '" style="background-color: cadetblue;">';
      html += '  <td class="text-left col-md-6"><textarea rows="4" cols="50" name="case_intake_call_record[' + call_row + '][caller_name]"  placeholder="Enter Call Record" class="form-control" value="" id="input-call' + call_row + '" ></textarea></td>';
      html += '  <td class="text-right col-md-3"><div class="input-group" data-date-format="mm/dd/yyyy" data-date-end-date="0d"><input type="text" name="case_intake_call_record[' + call_row + '][caller_date]" placeholder="MM/DD/YYYY" class="form-control" value="'+new_today+'"> <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></div></td>';
      html += '  <td class="text-right col-md-3"><div class="input-icon"><i class="fa fa-clock-o"></i><input type="text" class="form-control" name="case_intake_call_record[' + call_row + '][caller_time]" value="'+time+'"></div></td>';
      html += '</tr>';
      $('#calls tbody').append(html);
      call_row++;
      clicks += 1;
      document.getElementById("autoCall").innerHTML = clicks;
      $("#autoCallhidden").val(clicks);  
      }
    
  3. View File Code

        <tr>
            <td class="text-left" colspan="4"><button type="button" onclick="addCallImage();" data-toggle="tooltip" title="" class="btn btn-primary"><i class="fa fa-plus-circle"></i></button>Click to Add Call Record</td>
        </tr>
    
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dounan9070 2018-02-10 05:02
    已采纳

    Each time the button click action occurs, you will want to make the function calls to nowDate. Try declaring the vars locally within the addCallImage() function so that they are reset every time the click occurs.

    已采纳该答案
    打赏 评论
  • drzil26260 2018-02-10 05:01

    Use new Date inside your function.So when function called then new Date and time generated. This answer from Mobile so answer maybe not in welll format. :)

    var nowDate = new Date(); var mm = nowDate.getMonth() + 1; mm = (mm < 10) ? '0' + mm : mm; var dd = nowDate.getDate(); dd = (dd < 10) ? '0' + dd : dd; var new_today = mm + '/' + dd + '/' + nowDate.getFullYear(); var mi = nowDate.getMinutes(); mi = (mi < 10) ? '0' + mi : mi; var time = nowDate.getHours() + ":" + mi;

    打赏 评论

相关推荐 更多相似问题