簡體   English   中英

Textarea值等於“”

[英]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.");

句子“請描述發生了什么。” 無論我在表單中的文本區域中鍵入什么內容,都會出現。

我對此很陌生,還沒有找到適合我的解決方案。

在您發表評論之后,我認為您只需在讀取函數上初始化后就不更改vm的值,這就是為什么它保持為空的原因,並且您會收到消息“請描述發生了什么”。 我不知道您正在使用的插件,但我假設您的ReportDescription始終保持為空,這就是您的問題。

HTML:我已經在您的textarea中添加了一個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;
      }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM