使用keyup匹配输入元素

match input elements with keyup

本文关键字:输入 元素 keyup 使用      更新时间:2023-09-26

我不确定这里发生了什么。。我试图只匹配变量输入的val,每次尝试,我都会得到一个对象,一个对象。

这是小提琴:http://jsfiddle.net/GLnQx/1/

是这段脚本把我搞砸了:

$("input.numberOfAccounts").keyup(function () {
$("input.pricingPerAccount").val($(newPricePerAccount));
});

newPricePerAccount会提醒我正确的数字,但当我试图将其放入val时,这是不好的。

我在这里错过了什么?

$()是jQuery选择器方法。newPricePerAccount是5050,所以它试图选择一个5050元素(即什么都没有),并返回一个对象。我想你只是想要

.val(newPricePerAccount)

应该是

$("input.pricingPerAccount").val(newPricePerAccount);

看看你的jsFiddle,newPricePerAccount是一个JS变量,对吗?在这种情况下,您不需要$()选择器

$仅用于选择DOM元素,而非变量:

$('input.pricingPerAccount').val(newPricePerAccount);