動態(tài)控制復選框(checkbox和radio)

實現(xiàn)的效果是共用一個頁面(添加和修改),默認為checkbox
添加為checkbox,修改為radio
前端頁面展示如下

<tr class="roleList">
                    <th>
                        管理角色: 
                    </th>
                    <td>
                        <#assign roleSet = (admin.roleSet)! />                      
                            <#list allRoleList as role>
                                    <label>
                                        <input type="checkbox" name="roleList.id" id="rolest" value="${role.id}"<#if (roleSet.contains(role))!> checked</#if> />${role.name}
                                    </label>
                            </#list>
                            <label class="requireField">*</label>                                               
                    </td>
                </tr>
function add(){     
            localStorage.isUpdate = "false";
            location.href='team_data!add.action';
            
        }
        function update(){
            var s = $("#infoMarket").datagrid('getSelections');
            if(s == null || s == "") {
                return $.messager.alert("提示", "必須選中一行");
            } else if(s.length > 1) {
                return $.messager.alert("提示", "操作錯誤,只能選中一行");
            }
            var keyid = $("#infoMarket").datagrid('getSelections')[0].keyid;
            localStorage.isUpdate = "true";     
            location.href="team_data!edit.action?id="+keyid;
            

        }

js控制

if(localStorage.isUpdate=="true"){
        var id = document.getElementsByName('roleList.id');
                var value = new Array();
                for(var i = 0; i < id.length; i++){
                id[i].type="radio";
                }
        }else{
            var id = document.getElementsByName('roleList.id');
            var value = new Array();
            for(var i = 0; i < id.length; i++){
            id[i].type="checkbox";
            }           
        }

設置一個全局變量,點擊頁面通過修改type來控制復選框的狀態(tài)
獲取復選框的值

var id = document.getElementsByName('roleList.id');
     var value="";
     for(var i = 0; i < id.length; i++){
      if(id[i].checked)
      //value.push(id[i].value);
      value+=id[i].value+",";
     }
     value=value.substring(0,value.length-1);
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容