我将禁用提交按钮,直到输入内容。问题在于 Enter 键被计为字符或“长度”。
这是一个TextArea
Count.js.coffee
$(document).ready ->
$(".cmnt_btn").attr "disabled", true
$(".cmnt_area").keyup ->
# Disable button when nothing is entered
# Enter Key tricks this
unless $(this).val().length is 0
$(".cmnt_btn").attr "disabled", false
else
$(".cmnt_btn").attr "disabled", true
# Character Count
left = 300 - $(this).val().length
left = 0 if left < 0
$(".cmnt_counter").text left
如何“禁用”输入键作为字符计数或禁用该键直到输入其他内容?
最佳答案
您可能要求从文本区域的开头和结尾 trim 新行/空格:
$(this).val().replace(/^\s+|\s+$/g, '').length
此外,在禁用属性上设置 true/false 不会执行任何操作:用 prop 替换它们。
关于javascript - 输入关键技巧 val() length/jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23375854/