Limit 1 or n choices for the Multiple Selection List Box (InfoPath + SharePoint + JavaScript)
来源:互联网 发布:windows 2008 asp 编辑:程序博客网 时间:2024/06/03 17:21
Our client wants to use a MultipleSelection List Box in the custom list form (designed by InfoPath), but he wantto limit number of the choices, the user only can select one or two choices,then the question comes out, there isn’t any out box function for InfoPath andSharePoint to only allow user select number of choices, so we need to writesome code to implement this requirement.
If I write code in C# to implement this function, we need to deploy the dll to GACof every SharePoint EF and App server, client don’t like that since securityreason.
Atlast I decide to use JavaScript to implement this requirement, but it is noteasy to do, I have tired some ways before I found the correct way:
1. My first try is use Query toadd event in the document ready function:
$(document).ready(function () { checkboxes = $("input[type='checkbox']"); if (checkboxes.length != 0) { checkboxes.each( function () { var oldAttributeValue = $(this)[0].attributes["onClick"].nodeValue; var newAttributeValue = "return CheckedCheckBox(this) && (MultiSelectListBoxCollection.OnClick(this, event)); "; if (oldAttributeValue != newAttributeValue) $(this)[0].setAttribute("onClick", newAttributeValue); }); }); function CheckedCheckBox(checkBox, event) { if (checkBox.checked) { var checkedCount = 0; checkboxes = $("input[type='checkbox']"); checkboxes.each( function () { if ($(this)[0].checked) checkedCount++; }); if (checkedCount >= 3) { alert("Only can select one option"); checkBox.click(); return false; } } return true; }
But it can’t get the check box in the document ready function,the length ofcheckboxes always 0, that maycause by the check box control is load by Ajax.
2. The second way I tried is, add timer to add event:
$(document).ready(function () { timeout = setTimeout('timeout_trigger()', 1000); }); function timeout_trigger() { checkboxes = $("input[type='checkbox']"); // alert(checkboxes.length); if (checkboxes.length != 0) { checkboxes.each( function () { var oldAttributeValue = $(this)[0].attributes["onClick"].nodeValue; var newAttributeValue = "return CheckedCheckBox(this) && (MultiSelectListBoxCollection.OnClick(this, event)); "; if (oldAttributeValue != newAttributeValue) $(this)[0].setAttribute("onClick", newAttributeValue); }); if (timeout) { clearTimeout(timeout); timeout = null; } } } function CheckedCheckBox(checkBox, event) { if (checkBox.checked) { var checkedCount = 0; checkboxes = $("input[type='checkbox']"); checkboxes.each( function () { if ($(this)[0].checked) checkedCount++; }); if (checkedCount >= 3) { alert("Only can select one option"); checkBox.click(); return false; } } return true; }
That script doesn’t work either, since thecontrol refresh after the user do click one of the checkbox, it change the onClickto original value
3. The third approach I tried is using theDOMSubtreeModified
$(document).ready(function () { if (document.addEventListener){ document.addEventListener('DOMSubtreeModified', timeout_trigger); } else if (document.attachEvent){ document.attachEvent('DOMSubtreeModified', timeout_trigger); } }); function timeout_trigger() { checkboxes = $("input[type='checkbox']"); // alert(checkboxes.length); if (checkboxes.length != 0) { checkboxes.each( function () { var oldAttributeValue = $(this)[0].attributes["onClick"].nodeValue; var newAttributeValue = "return CheckedCheckBox(this) && (MultiSelectListBoxCollection.OnClick(this, event)); "; if (oldAttributeValue != newAttributeValue) $(this)[0].setAttribute("onClick", newAttributeValue); }); if (timeout) { clearTimeout(timeout); timeout = null; } } } function CheckedCheckBox(checkBox, event) { if (checkBox.checked) { var checkedCount = 0; checkboxes = $("input[type='checkbox']"); checkboxes.each( function () { if ($(this)[0].checked) checkedCount++; }); if (checkedCount >= 3) { alert("Only can select one option"); checkBox.click(); return false; } } return true }
It doesn’t work either.
4. The last one is the correctsolution to overwrite the original onlick event method for Multiple Selection List BoxMultiSelectListBoxCollection.OnClick
var checkboxes;var timeout;var oldFunction; $(document).ready(function () { oldFunction = MultiSelectListBoxCollection.OnClick; MultiSelectListBoxCollection.OnClick = CheckedCheckBox; }); function CheckedCheckBox(checkBox, event) { if(checkBox.checked) { var checkedCount = 0; checkboxes = $("input[type='checkbox']"); checkboxes.each( function(){ if($(this)[0].checked) checkedCount ++; }); if(checkedCount >= 3) { alert("Only can select one option"); checkBox.click(); return false; } } return oldFunction(checkBox, event); }
- Limit 1 or n choices for the Multiple Selection List Box (InfoPath + SharePoint + JavaScript)
- InfoPath works with SharePoint list
- InfoPath与SharePoint之(一) InfoPath和SharePoint List
- 转:How to submit the rows of a repeating table in InfoPath to a SharePoint list
- InfoPath与SharePoint之(二) 用InfoPath定制化SharePoint List
- 深入浅出SharePoint—部署使用InfoPath定制过的List
- Why Can't Users See The Pick List Selection Requests For Old Orders?
- Choices of the Gadgets
- Data Guard ORA-16810: multiple errors or warnings detected for the database
- L6305W: Image does not have an entry point. (Not specified or not set due to multiple choices.)
- Get the SharePoint groups in Infopath 2010 and SharePoint 2010 by current user without any code
- 随机的力量(1) - The power of random two choices
- 论文阅读笔记之ICML2012::The Landmark Selection Method for Multiple Output Prediction 基于代表性特征选取的多维度回归方法
- InfoPath display or hide some section according to whether current user belong to SharePoint group
- PKU1426 Find The Multiple (memory limit超过了。。。。)
- OpenLayers3-7-Box Selection
- Multiple selection button name
- Android Multiple Selection Spinner
- 数形结合彻底解决2个球100层楼摔坏的问题 .
- 火柴问题
- 创业之前,先读读这篇文章
- java JList
- [疯狂Java讲义精粹] 第三章|流程控制与数组
- Limit 1 or n choices for the Multiple Selection List Box (InfoPath + SharePoint + JavaScript)
- android源码解析--MessageQueue
- Android修改源代码控制永不锁屏
- 使用阿里云云服务器一年多的感受
- SQLite数据库操作
- 批处理:如何判断本地的某端口是否开放
- 基于MSMQ的WCF SOA中间件建设
- C#--,设计一个windows应用程序,实现不同运算。
- 设计精巧的openERP