简体   繁体   English

如何在jQuery中清除textarea值?

[英]How to clear a textarea value in jQuery?

I'm trying to validate keycode entry by adding an alert when a user types a key that isn't expected, and then clearing the text area. 我试图通过在用户键入不期望的键时添加警报,然后清除文本区域来验证键码输入。 The alert functions correctly but the text area isn't clearing. 警报功能正常,但文本区域未清除。

I've checked the documentation here , but I can't see an area with my .val() line. 我在这里检查了文档,但是我的.val()行看不到区域。 I've also tried this: $("#noteNumberInput").attr('value', ""); 我也尝试过这样: $("#noteNumberInput").attr('value', "");

Scenario: I type 1-9 in the text box and get no alert (works fine), if I type a letter az for example, the alert pops up but the letter remains in the text box. 场景:我在文本框中输入1-9,但没有任何警报(工作正常),例如,如果我输入字母az,则会弹出警报,但该字母仍保留在文本框中。

EDIT: 编辑:

Something I've noticed is that it does clear the textarea after the first key. 我注意到的是,它确实清除了第一个键后的文本区域。 If I type the letter 'a' and then 'b', the 'a' is removed and replaced with a 'b'. 如果我输入字母“ a”,然后输入“ b”,则将“ a”删除并替换为“ b”。

HTML: HTML:

<textarea id="noteNumberInput" placeholder="Note number"></textarea>

JS: JS:

var noteNumberInput = document.getElementById("noteNumberInput");

//VALIDATE NOTE NUMBER TEXTAREA
function validate(key) {
    var keycode = (key.which) ? key.which : key.keyCode;
    //comparing pressed keycodes
    if (keycode === 8 || (keycode >= 48 && keycode <= 57)) {
        return true;
    }
    if (keycode < 48 || keycode > 57) {
        alert("Please only enter the note number e.g. 1, 2..14 etc.");
        $("#noteNumberInput").val("");
        return false;
    }
}

noteNumberInput.addEventListener("keydown", validate);

When you do $("#noteNumberInput").val(''); 当您执行$("#noteNumberInput").val(''); , it removes all the content of the textarea, so if that's not what is happening, the problem is probably somewhere else. ,它会删除文本区域的所有内容,因此如果这不是正在发生的情况,则问题可能出在其他地方。

Using $("#noteNumberInput").val() will clear the textarea 使用$("#noteNumberInput").val()将清除文本区域

EDIT 编辑

The problem is the keydown handler. 问题是keydown处理程序。 In this case the function will be triggered followed by the display of alert & then the text area will be populated. 在这种情况下,将触发该功能,然后显示警报,然后将填充文本区域。 But on using keyup the function will be triggered on release of the key.So by that time the textarea will be populated with value. 但是在使用keyup该功能将在释放键时触发,因此届时textarea将填充值。

Change the keydown to keyup keydown更改为keyup

var noteNumberInput = document.getElementById("noteNumberInput");
noteNumberInput.addEventListener("keyup", validate);

DEMO 演示

Change noteNumberInput.addEventListener("keydown", validate); 更改noteNumberInput.addEventListener("keydown", validate); to use keyup 使用keyup

您只要求在按next键时才能真正执行validate()函数。

I think that´s not the best idea to trigger key events, because cut and paste and drag and drop can also change the input element. 我认为这不是触发键事件的最佳方法,因为剪切和拖放操作也可以更改输入元素。

try this: 尝试这个:

Element.addEventListener('input', function(){
    this.value=this.value.replace(/[^0-9,.]/g, '');
});

this must be adapted to textarea... 这必须适应textarea ...

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

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