我正在搜索以下事件,我需要在注册屏幕的文本框中输入的字符数自动显示在JavaScript或jQuery的同一文本框旁边。
这是初学者的问题,提前谢谢。
答案 0 :(得分:7)
收听akeys.length
事件,然后检查文本框值的长度。
var first = ["shoop", "doop", "woop", "loop"]
var second = ["hamp","damp", "samp", "wamp"]
console.log("recognized")
var returned = isEqual(first,second);
console.log(returned)
function isEqual(a, b) {
const aKeys = Object.getOwnPropertyNames(a)
const bKeys = Object.getOwnPropertyNames(b)
if (aKeys.length !== bKeys.length) {
return aKeys.every((key, index) => {
key === bKeys[index]
})
}
else{
return true
}
}
import Cocoa
func findAll<T: Equatable, C: Collection>(items: C, itemToFind: T) -> [C.Index]? where C.Iterator.Element == T {
guard items.contains(itemToFind) else {
return nil
}
var result = Array<C.Index>()
var index = items.startIndex
for item in items {
if item == itemToFind {
result.append(index)
}
index = items.index(after: index) // index = index.successor()
}
return result
// Another approach:
// return items.indices.filter { items[$0] == itemToFind }
}
print("\nList the Indicies within 'items' of where 'itemToFind' was found")
let Indexes1 = findAll(items: [5, 3, 7, 3, 9], itemToFind: 3) // result: [1, 3]
print(Indexes1!)
let Indexes2 = findAll(items: ["c", "a", "b", "c", "a"], itemToFind: "c") // result: [0, 3]
print(Indexes2!)
func offsetValue(input: String.CharacterView, position: String.CharacterView.Index) -> Int {
let offset = input.distance(from: input.startIndex, to: position)
return offset
}
var i = 0
let stringChars = "Hello, playground!".characters
let stringIndexes = findAll(items: stringChars, itemToFind: Character("l")) // result: [2, 3, 8]
print("[", terminator: "")
for stringIndex in stringIndexes! {
let stringIndexInt = offsetValue(input:stringChars, position: stringIndex)
print("\(stringIndexInt)", terminator: "")
i += 1
if i < (stringIndexes?.count)! {
print(", ", terminator: "")
}
}
print("]")
答案 1 :(得分:1)
恕我直言,按键应该是寻找字符数的理想事件。
$('#input').on('keypress', function(e) {
var count = $(this).val().length;
$('#span').text(count);
}
Keypress vs. Keyup 如果我按住任意一个字符键几秒钟,它就会在输入框中输入几个字符,而keyup事件会给你一个1的计数,因为键只被提升一次。但是,按键会输入确切的字符数。