使用复选框并在 Lightswitch 中计算它们 HTML

using checkboxes and counting them in Lightswitch HTML

我目前正在使用此代码将复选框显示为自定义控件,并且效果完美:

// Create the checkbox and add it to the DOM.
    var checkbox = $("<input type='checkbox'/>")
            .css({
                height: 20,
                width: 20,
                margin: "10px"
            })
            .appendTo($(element));

    // Determine if the change was initiated by the user.
    var changingValue = false;

    checkbox.change(function () {
        changingValue = true;
        contentItem.value = checkbox[0].checked;
        changingValue = false;
    });
    contentItem.dataBind("value", function (newValue) {
        if (!changingValue) {
            checkbox[0].checked = newValue;
        }
    });

但是现在我想稍微扩展一下,我想知道是否有人知道我如何根据值的真假来计算值。

我在找什么: 我下面有 2 个复选框,1st 是“TRUE”,2nd 是“"

所以示例数据将是:

var trueCount = 0;
var falseCount = 0;

window.alert("There are: " + trueCount + " values that are true and " + falseCount + " that are false");

和上面的例子trueCount = 1falseCount = 1

感谢人们给我的任何意见,非常感谢

我无法让它与自定义控件复选框一起使用,但对于标准开关,这段代码对我有用:

var trueCount = 0;
var falseCount = 0;

myapp.TableName.ColumnName_postRender = function (element, contentItem) { // count the true or false values contentItem.dataBind("value", function (newValue) {

    if (contentItem.value == true) {
        trueCount ++;
        falseCount--;


    } else if (contentItem.value == false) {
        falseCount++;
        trueCount--;
    }


});

//count 3 sets both trueCount and falseCount to 0 as they would already be affected by the 
//post render method. This works by adding or subtracting the amount of false values non 
//screen (works my my scenario)
var count3 = 0;

if (contentItem.value == false) {
    count3++;
}
falseCount = falseCount - count3;
trueCount = trueCount + count3;

};

myapp.TableName.Save_execute = function (screen) {
  window.alert("True Count: " + trueCount + " | falseCount: " + falseCount);

 //set both count values back to 0 for when the screen is next run
 trueCount = 0;
 falseCount = 0;
}