简体   繁体   English

无法使用 jquery 设置文本输入的值?

[英]Cannot Set value of the text input using jquery?

When the user add the question, I want to store it in database.One of the column of database is "Time" having datatype datetime.当用户添加问题时,我想将其存储在数据库中。数据库的一列是“时间”,数据类型为日期时间。

<div asp-validation-summary="All" role="alert" style="display:none"></div>
<div class="qs-container mt-5 mb-5 py-5">
        <h1>Ask a Public Question</h1>
        <form  method="post" action="/User/AskQuestion" id="question_form" class="d-flex justify-content-space-between align-items-center" style="flex-direction: column;">
            <div class="title-div">
                <label asp-for="Title">Question Title:</label>
                <br>
                <input type="text" asp-for="Title" id="title1">
                <br>
                <span asp-validation-for="Title" class="text-danger"></span>
            </div>
            <div class="qs-div">
                <label asp-for="Description">Description:</label>
                <br>
                <textarea asp-for="Description" id="summernote"></textarea>
                <br>
                <span asp-validation-for="Description" class="text-danger"></span>
            </div>
            <div class="opt-grp">
                <label asp-for="Subject">Choose Subject:</label>
                <br>
                <select asp-for="Subject" id="subject">
                    <option selected="selected" value="Exam">Exam</option>
                    <option value="Admission">Admission</option>
                    <option value="">Job</option>
                    <option value="Study">Study</option>
                    <option value="">Information</option>
                    <option value="">Events</option>
                </select>
            </div>
            <input asp-for="QuestionaireId" value="@UserId"/>
            <input id="question_time" asp-for="Time"/>
            <button class="btn btn-success submit-btn" value="submit">Submit</button>
        </form>
    </div>

and now what i am doing is to change value of input before submitting the form.现在我正在做的是在提交表单之前更改输入的值。

    <script>
              $('#summernote').summernote({
        placeholder: 'Description goes here....',
        height:'25vh',
        toolbar: [
          ['style', ['style']],
          ['font', ['bold', 'underline', 'clear']],
          ['color', ['color']],
          ['para', ['ul', 'ol', 'paragraph']],
          ['table', ['table']],
          ['insert', ['link', 'picture']],
          ['view', ['codeview', 'help']]
        ]
      });
      $('#question_form').submit(function(){
          let currdate = new Date();
          let currdatetime=currdate.toLocaleString();
          $('#question_time').val(currdatetime)
          alert($('#question_time').val());
          return true;
      })
    </script>

but the value of question_time after submitting is still null.and database give this error:但是提交后question_time的值仍然是null.and数据库给出这个错误:

SqlTypeException: SqlDateTime overflow. SqlTypeException:SqlDateTime 溢出。 Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.必须介于 1753 年 1 月 1 日上午 12:00:00 和 9999 年 12 月 31 日晚上 11:59:59 之间。

The exception you got is the format of date you pass to the date field is wrong.您得到的exception是您传递给date字段的日期格式错误。 By default date uses YYYY-MM-DD .默认日期使用YYYY-MM-DD So in order to retrieve date first convert date object to toISOString() then using substring get only date part.因此,为了检索日期,首先将日期 object 转换为toISOString()然后使用substring仅获取日期部分。

$('#question_form').submit(function(){
    let currdate = new Date();
    let currdatetime = currdate .toISOString().substr(0, 10);
    $('#question_time').attr('value',currdatetime);
    alert($('#question_time').val());
    return true;
});

More information in this Stackoverflow questionStackoverflow 问题中的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM