【发布时间】:2016-05-30 13:25:39
【问题描述】:
我有复选框,我需要在控制器功能中的按钮提交上获取该复选框的值:
<input id="Check" type="checkbox" ng-model="CheckValue" />
【问题讨论】:
标签: javascript jquery html angularjs
我有复选框,我需要在控制器功能中的按钮提交上获取该复选框的值:
<input id="Check" type="checkbox" ng-model="CheckValue" />
【问题讨论】:
标签: javascript jquery html angularjs
$scope.CheckValue 将在您的控制器中具有值。
在提交时执行console.log($scope.CheckValue),您将看到console 中的值。
【讨论】:
在你应该使用的控制器中
$scope.CheckValue
有时在控制器中定义您在模板中使用的属性也很好。因此,您将对变量有更清晰的概览和控制。
【讨论】:
function forceNumber(element) {
element
.data("oldValue", '')
.bind("paste", function(e) {
var validNumber = /^[-]?\d+(\.\d{1,2})?$/;
element.data('oldValue', element.val())
setTimeout(function() {
if (!validNumber.test(element.val()))
element.val(element.data('oldValue'));
}, 0);
});
element
.keypress(function(event) {
var text = $(this).val();
if ((event.which != 46 || text.indexOf('.') != -1) && //if the keypress is not a . or there is already a decimal point
((event.which < 48 || event.which > 57) && //and you try to enter something that isn't a number
(event.which != 45 || (element[0].selectionStart != 0 || text.indexOf('-') != -1)) && //and the keypress is not a -, or the cursor is not at the beginning, or there is already a -
(event.which != 0 && event.which != 8))) { //and the keypress is not a backspace or arrow key (in FF)
event.preventDefault(); //cancel the keypress
}
if ((text.indexOf('.') != -1) && (text.substring(text.indexOf('.')).length > 2) && //if there is a decimal point, and there are more than two digits after the decimal point
((element[0].selectionStart - element[0].selectionEnd) == 0) && //and no part of the input is selected
(element[0].selectionStart >= element.val().length - 2) && //and the cursor is to the right of the decimal point
(event.which != 45 || (element[0].selectionStart != 0 || text.indexOf('-') != -1)) && //and the keypress is not a -, or the cursor is not at the beginning, or there is already a -
(event.which != 0 && event.which != 8)) { //and the keypress is not a backspace or arrow key (in FF)
event.preventDefault(); //cancel the keypress
}
});
}
forceNumber($("#myInput"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="myInput" />
【讨论】: