jQuery从表单字段中获取值,并用它们做数学运算函数不起作用
jquery get values from a form field and do math with them function not working
我在jquery中有一个函数,在keyup上,它应该对来自2个字段的值进行数学运算,并使用总权重ID更改我的总权重字段的值,如下所示:
j查询:
$('#numberOfItems').keyup(function(){
var items = $('#numberOfItems').val();
var weightitem = $('#weightitem').val();
var totalweight = items * weightitem;
$('#totalweight').val(totalweight);
});
.HTML:
<div>
<form>
<input type="text" id=weightitem"" placeholder="Weight per item">
<input type="text" id=numberOfItems"" placeholder="Items">
<input type="text" id=totalweight"" placeholder="Weight in total">
</form>
</div>
但这行不通...谁能帮我解决这个问题?
提前谢谢你!
我以前遇到过同样的问题,但我不明白为什么会发生这种情况。但是我已经通过使用 $ 符号而不是 var 声明变量来解决它。
$('#numberOfItems').keyup(function(){
$items = $('#numberOfItems').val();
$weightitem = $('#weightitem').val();
$totalweight = $items * $weightitem;
$('#totalweight').val($totalweight);
});
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hide</title>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<style type="text/css">
label input[type="file"]
{
display: block;
margin-top: -20px;
opacity: 0;
}
</style>
<script>
var items=0;
var weightitem=0;
var totalweight=0;
$('#numberOfItems').keyup(function(){
var items = $('#numberOfItems').val();
var weightitem = $('#weightitem').val();
var totalweight = items * weightitem;
$('#totalweight').val(totalweight);
});
$(window).ready(function() {
$(document).delegate('#numberOfItems','keyup',function(){
items = $(this).val();
weightitem = $('#weightitem').val();
totalweight = items * weightitem;
$('#totalweight').val(totalweight);
});
$(document).delegate('#weightitem','keyup',function(){
items = $('#numberOfItems').val();
weightitem = $(this).val();
totalweight = items * weightitem;
$('#totalweight').val(totalweight);
});
});
</script>
</head>
<body>
<input type='text' id='numberOfItems'>
<input type='text' id='weightitem'>
<input type='text' id='totalweight'>
</body>
</html>
相关文章:
- 监视函数从服务返回不起作用,但作用域函数起作用
- Ember Data DS.Model's set函数不起作用
- Javascript:If-then语句在函数中不起作用
- JavaScript链接在点击时不起作用;函数在页面加载时工作
- AngularJS指令部分应用的函数don'不起作用
- 为什么我的JavaScript堆栈排序函数不起作用
- 嵌套到另一个函数中的Fancybox函数;不起作用
- Javascript onchange()函数不起作用
- I'我试图在文本区域中进行特定的输入,调用特定的javascript函数,但没有成功;不起作用
- javascript函数调用不起作用
- 另一个Ajax函数触发的Ajax函数不起作用
- 从PHP调用JS函数不起作用
- 清除函数中if语句内部不起作用的间隔
- js函数堆栈传入变量,.hide()不起作用
- jQuery validate函数不起作用
- jQuery克隆函数在chrome中不起作用
- JS-窗口宽度函数不起作用
- 函数调用不起作用
- .hasClass 在 onclick 函数中不起作用,但删除和添加类函数可以
- javascript函数,该函数不起作用,但不会显示任何错误