[英]Button text and property value change when button is clicked in JavaScript
I have a input type of button. 我有按钮的输入类型。 Button is something like this: Button是这样的:
<h2>RFI Status Type</h2>
<input type="button" class="btn btn-small btn-primary" value="New RFI Status" data-bind="click: $root.createRFIStatusType" />
<hr />
<table class="search">
<tr class="table_title">
<th>RFI Status Type Name</th>
<th>Sequence</th>
<th>Active Status</th>
</tr>
<tbody data-bind="foreach: RFIStatusTypes">
<tr class="table_data">
<td><a data-bind="text: RFIStatusTypeName, click: $parent.editRFIStatusType">Edit</a></td>
<td data-bind="text: Sequence"></td>
<td>
<input type="button" id="chkActiveStatus" data-bind = "value: activeStatus(ActiveStatus) "/>
</td>
</tr>
</tbody>
</table>
The JavaScript function activeStatus(ActiveStatus)
is: JavaScript函数activeStatus(ActiveStatus)
是:
<script>
function activeStatus(ActiveStatus)
{
if (ActiveStatus == 0) {
return "Deactivate";
}
else {
return "Activate";
}
}
</script>
What it is doing is that, it is taking the value of ActiveStatus, which is a column in my database table. 它正在做的是,它采用ActiveStatus的值,该值是我数据库表中的一列。 The value of ActiveStatus is either 1 or 0. When it takes the value 0, it returns the value of the button as "Deactivate", otherwise "Activate". ActiveStatus的值为1或0。当取值为0时,它将返回按钮的值为“停用”,否则为“激活”。
When I click the button, I want to change the button value from "Activate" to "Deactivate" and vice versa, and at the same time, the corresponding ActiveStatus value will also change. 当我单击按钮时,我想将按钮值从“激活”更改为“禁用”,反之亦然,同时,相应的ActiveStatus值也将更改。 If ActiveStatus is 0, it will change to 1; 如果ActiveStatus为0,它将更改为1;否则,将更改为1。 if it is 1 then it will change to 0. 如果为1,则它将更改为0。
How can I do this? 我怎样才能做到这一点? I have tried this multiple times and failed. 我已经尝试了多次,但失败了。 I tried an onclick event in my input but it didn't work. 我在输入中尝试了onclick事件,但没有成功。
EDIT-1: I've written the JavaScript code inside my view page and have modified it a bit but still not getting the desired result. EDIT-1:我已经在视图页面中编写了JavaScript代码,并对其进行了一些修改,但仍未获得所需的结果。 Here it goes: 它去了:
<script>
function activeStatus(ActiveStatus)
{
if (ActiveStatus == 0) {
ActiveStatus++;
return "Deactivate";
}
else if (ActiveStatus == 1)
{
ActiveStatus--;
return "Activate";
}
}
</script>
EDIT-2: Many have been mentioning that I haven't included knockout.js code here. EDIT-2:许多人提到我没有在此处包括基因敲除.js代码。 So here is the RFIStatusType.js code: 因此,这是RFIStatusType.js代码:
var RFIStatusTypesViewModel = function () {
var self = this;
var url = "/RFIStatusType/GetAllRFIStatusType";
var refresh = function () {
$.getJSON(url, {}, function (data) {
self.RFIStatusTypes(data);
});
};
// Public data properties
self.RFIStatusTypes = ko.observableArray([]);
// Public operations
self.createRFIStatusType = function () {
window.location.href = '/RFIStatusType/RFIStatusTypeCreateEdit/0';
};
self.editRFIStatusType = function (RFIStatusType) {
window.location.href = '/RFIStatusType/RFIStatusTypeCreateEdit/' + RFIStatusType.RFIStatusTypeID;
};
self.removeRFIStatusType = function (RFIStatusType) {
// First remove from the server, then from the UI
if (confirm("Are you sure you want to delete this RFI Status?")) {
var id = RFIStatusType.RFIStatusTypeID;
$.ajax({ type: "DELETE", url: 'RFIStatusType/DeleteRFIStatusType/' + id })
.done(function () { self.RFIStatusTypes.remove(RFIStatusType); });
}
}
refresh();
};
ko.applyBindings(new RFIStatusTypesViewModel());
EDIT-3: Now my button is this: EDIT-3:现在我的按钮是这样的:
<input type="button" id="chkActiveStatus" data-bind="value: activeStatus(ActiveStatus), click: toggleActiveStatus(ActiveStatus)" />
And my JavaScript is re-written here: 我的JavaScript在这里重写:
<script>
function activeStatus(ActiveStatus)
{
if (ActiveStatus == 0)
{
alert("ActiveStatus is now 0");
return "Activate";
}
else if (ActiveStatus == 1)
{
alert("ActiveStatus is now 1");
return "Deactivate";
}
}
function toggleActiveStatus(ActiveStatus)
{
if (ActiveStatus == 0)
{
ActiveStatus++;
alert("ActiveStatus is now 1");
return ActiveStatus;
}
else if (ActiveStatus == 1)
{
ActiveStatus--;
alert("ActiveStatus is now 0");
return ActiveStatus;
}
else alert("Error");
}
</script>
But still no luck. 但是仍然没有运气。
As Govan seems the most correct, you cannot just grab a global function within the data-bind concept of knockout. 由于Govan似乎是最正确的,您不能仅仅在淘汰的数据绑定概念中获取全局功能。 'value' will only set the value of the button...which isn't really relevant. “值”将仅设置按钮的值……这并不是很重要。 data-bind='click: activeStatus' is how you want to handle your click event...but the function activeStatus() needs to be declared where you declare the object type defining each instance of RFIStatusTypes. data-bind ='click:activeStatus'是您要处理click事件的方式...但是需要在声明用于定义RFIStatusTypes每个实例的对象类型的地方声明函数activeStatus()。
Provide more of your knockout code and this example may magically show more information as well. 提供更多的剔除代码,此示例也可以神奇地显示更多信息。
Personally, I smell a good example of a custom bindingHandler coming on. 就个人而言,我闻到了一个很好的自定义bindingHandler实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.