Question

How to make a checkbox column not to allow multiple values Only check one option!

Would appreciate any help

Était-ce utile?

La solution

If you're on SP 2013, use JSLink and add the jquery/javascript to allow selection of one checkbox (in post Render handler) the NewForm and EditForm.

SP renders the checkboxes within individual TDs that have a parent table with your column name + guid. For ex. If you have a column named 'MyCheckbox' MyCheckbox_e43ed65a-00e8-4560-b59c-c7fa0f923a4d_MultiChoiceTable. Use jQuery to bind an event to all children checkboxes and do a count. If selected checkboxes is greater than 1, return false on other checkbox click event.

Here's a sample script:

$("input[id^='MyCheckbox_']").on('click', function () {
    var count = $("table[id^='MyCheckbox_'] [type='checkbox']:checked").length;
    window.console && console.log(count);
    if (count > 1)
        return false;
});

Autres conseils

You can probably do this through javascript if you want, as it's pretty easy to just hide boxes. I'll write an example. Let's say your checkboxes are defined by the name "col_checkbox".

var target = document.getElementsByName("col_checkbox");  //Now we have all the checkboxes in a node array
var checkFlag = false;
for(i = 0; i < 5; i++) {

  if(target[i].checked) {   //cycle through all boxes, seeing if one is checked
    checkFlag = true;
    for(j = 0; j < i; j++) {
       target[j].style.display = 'none';
       }
    for(k = i; k < 5; k++) {
       target[k].style.display = 'none';
       }

    //So we found one that is checked, and we hide all the other boxes
    //And if no value found, checkFlag is false
    }
 }

 if(!checkFlag) {
   for(i = 0; i < 5; i++) {
      target[i].style.display = 'block';  //if no box checked, display all boxes
      }

Pretty simple in design, just tailor it to your needs.

Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top