文本区域值等于“”

Textarea value is equal to ""

我希望能够根据用户输入的内容更改 textarea 标签内的值。

<div class="form-group">
    <label>Describe what happened</label>
    <textarea id="descriptionReport" form="newReport" placeholder="Describe what happened.." name="description" data-rule-describeEvent="true " cols="5" rows="5" class="form-control" maxlength="255" a></textarea>
</div>

我在我的脚本中声明了一个变量:

$(document).ready(function () {
   var vm = {
       movieIds: [],
       ReportDescription: null
  };

然后我将它设置为 textarea 标签内的值

vm.ReportDescription = document.getElementById("descriptionReport").value;

我总是得到的值等于“”。

我添加了一个验证插件来检查值是否有效:

$.validator.addMethod("describeEvent", function () {
            return vm.ReportDescription !== "";
        }, "Please describe what happened.");

无论我在表单的文本区域中键入什么,句子 "Please describe what happened." 都会出现。

我对此很陌生,一直无法找到适合我的解决方案。

在您发表评论后,我认为您在读取函数上初始化后根本没有更改 vm 的值,这就是为什么它保持为空并且您收到该消息的原因 "Please describe what happened"。我不知道您正在使用的插件,但我认为您的 ReportDescription 一直保持为 null,这就是您的问题。

HTML:我已经在您的文本区域中添加了一个 keyup 属性。

 <textarea id="descriptionReport" form="newReport" placeholder="Describe what happened.." name="description" data-rule-describeEvent="true " cols="5" rows="5" class="form-control" maxlength="255" onkeyup="applyChanges()"></textarea>

在您的代码中添加此函数。它会在用户按下某个键后更改 vm.ReportDescription 的值。

var vm;
$(document).ready(function () {
   vm = {
       movieIds: [],
       ReportDescription: null
  };
});
function applyChanges() { 
      vm.ReportDescription = document.getElementById("descriptionReport").value; 
      if (vm.ReportDescription == ""){
          vm.ReportDescription = null;
      }
}