从Checkboxes和Selects中获取值的总和 - KnockoutJS

时间:2015-04-10 07:14:25

标签: javascript jquery html css knockout.js

我已经在jQuery中做了这个:JSFiddle

HTML:

<div class="container">
    <header>
         <h3>Crazy Sh*t we'll do for Money</h3>

        <div class="small"><em>An elegant way to test out Knockout JS you're thinking of doing</em>

        </div>
    </header>
    <ul>
        <li>
            <input type="checkbox" name="services" value="150" id="srv" />
            <span>Throw knives at us while we're strapped on a turn table ($150)</span>

        </li>
        <li>
            <input type="checkbox" name="services" value="200" id="srv" />
            <span>Watch us get chased by Bulls ($200)</span>

        </li>
        <li>
            <input type="checkbox" name="services" value="799.50" id="srv" />
            <span>Watch us in a straight jacket immersed in a pool of electric Eels and try to get out of it Houdini style. ($799.50)</span>

        </li>
    </ul>
    <hr/>
    <div class="marginise">
<strong>I want to donate to your hospital bills</strong>

        <select id="donate" name="donate">
            <option value="0">No Thanks</option>
            <option value="100">$100</option>
            <option value="300">$300</option>
            <option value="500">$500</option>
        </select>
        <p><strong>Donations</strong>: <span id="blah"></span>
        </p>
    </div>
    <div class="est">
         <h4>Total</h4>
 <span id="serviceTotal"> </span>

    </div>
    <div class="f">These Events / Entertainment Services are not real. Unfortunately.</div>
</div>

jQuery的:

function outputValue() {
    mathUsage();
    var donValues = $("#donate").val(); //take from Select
    $("#blah").html(numeral(donValues).format('$0,0[.]00'));
}
var $cap = $('input[name="services"]'); //set VAR for checkbox values

function mathUsage() {
    var total = $("#donate").val(); // total (0) + donate value
    $cap.each(function () {
        if (this.checked) total = parseFloat(total) + parseFloat(this.value);
    });
    $("#serviceTotal").text(numeral(total).format('$0,0[.]00'));
}
//NumeralJS -> numeral(1000).format('0,0');

$("select").change(outputValue);
outputValue();

$cap.click(mathUsage);

//Now... how to Knockout? :/

我正在尝试练习的是在knockoutJS中创建它,这样我就可以拥有一小组HTML标记。

最好的方法是什么?

我知道那里有很多线索,比如这个Fiddle输出一个数组中的复选框的值...我想使用相同的方法并将值设置为总和,但不知道如何开始或我应该使用什么语法。

2 个答案:

答案 0 :(得分:1)

我认为你需要跟随:

&#13;
&#13;
var ViewModel = function(first, last) {
    this.first = ko.observable(first);
    this.last = ko.observable(last);
 
    this.total = ko.computed(function() {
        
        return parseFloat(this.first())  + parseFloat(this.last());
    }, this);
};
 
ko.applyBindings(new ViewModel("10", "10")); 
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<script src="http://knockoutjs.com/downloads/knockout-3.2.0.js"></script>
<div class='liveExample'>   
    <p>First: <input data-bind='value: first' /></p> 
    <p>Last: <input data-bind='value: last' /></p> 
    <h2> <span data-bind='text: total'> </span></h2>  
</div>
&#13;
&#13;
&#13;

检查Fiddle.

答案 1 :(得分:1)

我首先给每个复选框提供自己的ID /名称:

<ul>
        <li>
            <input type="checkbox" name="knives" value="150" id="knives" data-bind="checked: knives" />
            <span>Throw knives at us while we're strapped on a turn table ($150)</span>

        </li>
        <li>
            <input type="checkbox" name="bulls" value="200" id="bulls" data-bind="checked: bulls"  />
            <span>Watch us get chased by Bulls ($200)</span>

        </li>
        <li>
            <input type="checkbox" name="eels" value="799.50" id="eels" data-bind="checked: eels" />
            <span>Watch us in a straight jacket immersed in a pool of electric Eels and try to get out of it Houdini style. ($799.50)</span>

        </li>
    </ul>

那么您的视图模型就是这样:

var viewModel = function() {
    var self = this;
    self.knives = ko.observable(false);
    self.bulls = ko.observable(false);
    self.eels= ko.observable(false);

    self.total = ko.computed(function() {
      var i = 0;
      if (self.knives())
        i += 150;
      if (self.bulls())
        i += 200;
      if (self.eels())
        i += 799.50;    
      return total; 
    });
};

ko.applyBindings(new viewModel()); 

这是一般的想法。您可以添加其他viewModel属性来保存每个服务的值,或者创建一个小对象来包含每个“服务”的值和状态。我会留给你的。