使按钮像复选框一样工作,挖空js

Make button act like checked box, knockout js

本文关键字:工作 一样 挖空 js 按钮 复选框      更新时间:2023-09-26

所以基本上我的html上有一个按钮,上面有一个小js,点击时按钮变绿,再次点击时按钮变灰,让它感觉像打开和关闭。这是按钮的JS和HTML代码

<script>
$('#button1').click(function() {
    //Now just reference this button and change CSS
     $(this).toggleClass('buttonClassA');
});
</script>
<button type="button" class="col-md-2 notice-btns btn " id="button1">
            <span class="glyphicon glyphicon-phone" aria-hidden="true"></span>
            <p data-bind="if : $data.emailNotification == 1 ">On</p>
            <p data-bind="if : $data.emailNotification == 0 ">Off</p>
          </button>

我有两个敲击函数,单击按钮时需要调用哪些是

self.emailNotification = ko.observable(); 
        self.turnOnEmailNotification = function () {
            $.ajax({
                type: 'POST',
                url: BASEURL + 'index.php/myprofile/checkNotificationValue/' + auth  + "/" + self.emailNotification(1) ,
                contentType: 'application/json; charset=utf-8'
            })
            .done(function(notifications) {
                self.emailNotification.removeAll();
                self.emailNotification.push(notifications);
            })
            .fail(function(xhr, status, error) {
                alert(status);
            })
            .always(function(data){                 
            });
        };
        self.turnOffEmailNotification = function () {
            $.ajax({
                type: 'POST',
                url: BASEURL + 'index.php/myprofile/checkNotificationValue/' + auth  + "/" + self.emailNotification(0) ,
                contentType: 'application/json; charset=utf-8'
            })
            .done(function(notifications) {
                self.emailNotification.removeAll();
                self.emailNotification.push(notifications);
            })
            .fail(function(xhr, status, error) {
                alert(status);
            })
            .always(function(data){                 
            });
        };

我尝试检查数据绑定,但后来丢失了按钮,因此需要帮助才能调用两个 ajax 调用

$root.打开电子邮件通知

$root.关闭电子邮件通知

在同一个按钮中,所以我可以给它一种打开和关闭的感觉。

Checked不是按钮元素的有效属性。相反,您可以使用click通过切换来实现相同的目的。

视图:

<button type="button" data-bind="click:tgle,style:{'background-color':emailNotification()==0 ? 'red':'green'}">
<p data-bind="text:$data.emailNotification() == 1 ? 'On' : 'Off' "></p>
</button>

查看型号:

var ViewModel = function() {
  var self = this;
  self.emailNotification = ko.observable(0);
  self.tgle = function() {
    self.emailNotification(self.emailNotification() == 1 ? 0 : 1)
    switch (self.emailNotification()) {
      case 0:
        console.log('turn off ajax fired');
        //turnOFFEmailNotification 
        //$.ajax({
        //  Do ajax stuff               
        //});
        break;
      case 1:
        console.log('turn on ajax fired');
        //turnOnEmailNotification //default off
        //$.ajax({
        //  Do ajax stuff               
        //});
        break;
      default:
        break;
    }
  }
};
ko.applyBindings(new ViewModel());

工作样品可供抓取