Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将html字段值传递给JQuery_Jquery_Html - Fatal编程技术网

将html字段值传递给JQuery

将html字段值传递给JQuery,jquery,html,Jquery,Html,当尝试从HTML字段获取数据时,我收到一个NaN错误 如果在代码中添加了数字值,则在测试时查询将起作用,并显示带有返回值的警报 <label>Monthly Cost</label> <input type="number" name="pricepermonth" id="pricepermonth" class="form-control" > <label>ProRata Cost</label> <input type="

当尝试从HTML字段获取数据时,我收到一个NaN错误

如果在代码中添加了数字值,则在测试时查询将起作用,并显示带有返回值的警报

<label>Monthly Cost</label>
<input type="number" name="pricepermonth" id="pricepermonth" class="form-control" >
<label>ProRata Cost</label>
<input type="text" name="remainingprice" id="remainingprice" class="form-control" >

预期的结果是,当向“每月成本”字段中添加数值时,将触发查询,然后查询将在第二个字段中按比例输出该值


我收到的唯一错误消息是NaN(不是数字)错误。

因为您使用的是输入,所以您应该使用它来:

获取第一个匹配元素的value属性的内容

在代码中:

var pricepermonth = $('#pricepermonth').val();
它用于其他用途:

存储与匹配元素关联的任意数据或返回 集合中第一个元素的命名数据存储中的值 匹配元素


换句话说,它有助于将各种对象、字符串、数组等与DOM元素关联

代码上的触发事件在哪里?我认为我们缺少一些代码?
$(“#pricepermonth”)。数据
可能应该是
$(“#pricepermonth”).data()
,虽然它看起来有点多余,因为
#pricepermonth
似乎实际上没有任何数据。你的逻辑什么时候运行?还有,
code()
函数在做什么?@chrisBinstead在哪一行出现错误抱歉,我看到jquery的顶部缺少一些行:$('pricepermonth').change(function(){$('remainingprice').val($(this.val());});功能代码(pricepermonth){感谢您的输入,我已经做了更改,现在返回的val是0,看起来它没有获取.val。我已经设置了一个JSFIDLE进行测试:酷,使用更新的代码,我已经将它添加到我正在构建的应用程序中,每月的成本基于通过AJAX获取的数据,并且正在工作,现在只是想看看如何推送.val而不是alert窗口到按比例费率的字段。
var pricepermonth = $('#pricepermonth').val();