【问题标题】:Change the name of toggle button in Knockout更改 Knockout 中切换按钮的名称
【发布时间】:2016-09-03 05:50:39
【问题描述】:

我有一个按钮应该有“开始”作为值然后如果按钮被点击 应该改为“暂停”

> <script> let isOn = false;

    fucntion startPause() {

       if (!isOn){
         // do something
         isOn = true;    }else{
          // do something else
        isOn = false; }

    // ... </script>



    <button data-bind ="click: startPause" ></button>

【问题讨论】:

    标签: javascript knockout.js


    【解决方案1】:

    这是一个例子: https://jsfiddle.net/kyr6w2x3/70/

    HTML:

    <button type="button" class="btn btn-default" data-bind="text:buttonText,click:startPause">
    

    JS:

     function AppViewModel() {
          var self = this;
          self.buttonText = ko.observable('Start');
            self.startPause = function (){
               // here you can have your own logic to toggle the value
            self.buttonText(self.buttonText().toUpperCase() === 'START' ? 'Pause' : 'Start');
          }
        }
     ko.applyBindings(new AppViewModel());
    

    【讨论】:

      【解决方案2】:

      使用文本绑定并使用 observable 更改其在模型上的值。

      参考:The "text" binding

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多