【发布时间】:2018-01-15 15:47:45
【问题描述】:
我有一个数量输入框,用户可以通过单击 - 和 + 按钮来更改它的值。问题是,如果我直接在输入框中输入值,我可以将此值解析到我的控制器。如果我通过单击按钮 + 或 - 更改值,我无法将值解析到控制器,它始终保持旧值。谁能帮帮我?
<button
onclick="var result = document.getElementById('qty'); var qty = result.value; if( !isNaN( qty ) && qty > 0 ) result.value--;return false;"
class="reduced items-count"
type="button"
>
<i class="fa fa-minus"> </i>
</button>
<input
type="text"
class="input-text qty"
title="Qty"
maxlength="12"
id="qty"
name="qty"
ng-init="qty='1'"
ng-model="qty"
>
<button
onclick="var result = document.getElementById('qty'); var qty = result.value; if( !isNaN( qty )) result.value++;return false;"
class="increase items-count"
type="button"
>
<i class="fa fa-plus"> </i>
</button>
还有我的控制器,
$scope.$watch('qty', function (val) {
if (val) {
alert("qty2:" + val);
}
});
【问题讨论】:
-
这段代码真的很难读。如果可能,请至少创建一个Minimal, Complete, and Verifiable example