Click here for addition f-6ren">
gpt4 book ai didi

javascript - 通过比较两个输入字段中的值启用/禁用元素

转载 作者:行者123 更新时间:2023-11-30 20:53:11 27 4
gpt4 key购买 nike

我有两个输入字段值,想知道如果第一个输入值大于第二个输入值,我该如何禁用按钮。它仅适用于第一个字母,但超过 2 个数字无效。

<button type="submit" data-bind="click: addition, enable:temp1() > temp2()" >Click here for addition</button>

 function ViewModel () {
var self = this;
self.var1 = ko.observable(0);
self.var2 = ko.observable(0);
self.temp1 = ko.observable(self.var1())
self.temp2 = ko.observable(self.var2())

this.addition = function() {
self.var1(Number(self.temp1()));
self.var2(Number(self.temp2()));

this.var3 = self.var1() + self.var2();
alert("Addition is = "+ this.var3 );
};
};

var vm = new ViewModel();
ko.applyBindings(vm);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<p>First #: <span data-bind = "text: var1" > </span></p>
<p>Sectond #: <span data-bind = "text: var2" > </span></p>

<p>Enter first #: <input data-bind = "value: temp1" /></p>
<p>Enter second #: <input data-bind = "value: temp2" /></p>


<p><button type = "submit" data-bind = "click: addition" enable="temp1() < temp2()" >Click here for addition</button></p>

最佳答案

提交按钮上的数据绑定(bind)属性有误。改变这个:

data-bind = "click: addition" enable="temp1() < temp2()"

为此:

data-bind = "click: addition, enable:Number(temp1()) < Number(temp2())"

此外,将 observable 包装在 Number 构造函数中会强制该值为数字。这是 the enable binding 的文档

关于javascript - 通过比较两个输入字段中的值启用/禁用元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47947259/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com