0

我目前使用此代碼,以顯示覆選框作爲一個自定義的控制,和它的作品完美:使用複選框,並在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個複選框,則是 「TRUE」 和第二是 「FALSE

  • 我希望能夠使用類似var count的東西來計算這些值,然後將其放在while循環或數組中,然後將其顯示回像下面這樣的按鈕以進行測試:window.alert("add in text here" + add_code_here)

enter image description here

所以例如數據將是:

var trueCount = 0; 
var falseCount = 0; 

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

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

感謝您的任何輸入的人可以給我,這是最欣賞

回答

0

我無法與它合作定製控制複選框但對於標準交換機此代碼爲我工作:

var trueCount = 0; 
var falseCount = 0; 

myapp.TableName.ColumnName_postRender =函數(元件,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; 
}