0

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

// 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 个复选框,第一个是“ TRUE ”,第二个是“ FALSE

  • 我希望能够使用 var count 之类的东西来计算这些值,然后将其放入 while 循环或数组中,然后将其显示在如下按钮上以进行测试:window.alert("add in text here" + add_code_here)

在此处输入图像描述

所以示例数据是:

var trueCount = 0;
var falseCount = 0;

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

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

感谢人们可以给我的任何输入,非常感谢

4

1 回答 1

0

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

var trueCount = 0;
var falseCount = 0;

myapp.TableName.ColumnName_postRender = function (element, contentItem) { // 计算真假值 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;
}
于 2015-01-15T13:39:47.603 回答