2014-10-06 166 views
0

我在jqgrid表中有两个下拉字段,指示开关状态为ON和OFF。jqgrid中的Onchange事件

如果用户在任何一个开关中选择“ON”选项,其他开关选项应该变为“OFF”,反之亦然。用户一次只能在交换机上启用。两个开关都可以处于'OFF'状态,但不能处于'ON'状态。因此,我需要上述的onchange事件,如果用户选择Switch1的'ON'选项,Switch2必须变为OFF,并且如果用户选择Switch2的'ON'选项,则Switch1必须变为OFF。请分享一些想法。

这里是我的代码

grid_data = [{'Switch_1':'OFF', 'Switch_1':'OFF'}, 
     {'Switch_1':'ON', 'Switch_1':'OFF'}, 
     {'Switch_1':'OFF', 'Switch_1':'ON'},] 
colNames:['Switch 1', 'Switch 2'], 
colModel:[ 
    {name:'Switch_1',index:'Switch_1', width:55,editable:  true,edittype:"select",editoptions:{width:20, value:"0:OFF;1:ON"}} 
    {name:'Switch_2',index:'Switch_2', width:55,editable: true,edittype:"select",editoptions:{width:20, value:"0:OFF;1:ON"}} 
     ] 

回答

0

你可以有一个jQuery事件监听下拉更改事件。在这一点上你可以做一些逻辑来确定状态。

$('#switch1').change(function(){ 
    if($('#switch1').val() == 'ON'){ 
     $('#switch2').val('OFF'); // you should put the value you set for off 
    } 
}); 

$('#switch2').change(function(){ 
    if($('#switch2').val() == 'ON'){ 
     $('#switch1').val('OFF'); // you should put the value you set for off 
    } 
});