2012-07-12 97 views
5

我在同一個無線電組裏有3個單選按鈕。他們是,如何知道選擇了哪個單選按鈕?

var rbutton1 = app.createRadioButton('dist','5 miles'); 

var rbutton2 = app.createRadioButton('dist','10 miles'); 

var rbutton3 = app.createRadioButton('dist','25 miles'); 

在事件處理函數,變量,e.parameter.dist給出真或假的只是基於rbutton3(最後一個單選按鈕)是否被選中與否。有沒有什麼辦法可以確定究竟選擇了哪個單選按鈕?

回答

2

使單選按鈕組像這樣工作的唯一方法是按照設計意圖使用它們,方法是在FormPanel中使用它們,並從表單的提交操作中查看doPost上的名稱(在您的案例中爲「dist」) 。

雖然有一些解決方法,使用新的客戶端處理程序,使其在任何面板上的單選按鈕使用情況與從大致相同。請在跟蹤器上查看this issue。您可能還想要明星這個問題,以跟蹤更新和投票的種類。

0

我用:

eventData.parameter.source 

,拿起使用addClickHandler的變化。

您需要存儲在某個地方,

0

這些按鈕假設是在異或模式。如果是這樣,他們需要有相同的名字。請查看Serge的answer以獲取詳細的解釋和示例代碼。

在我想出了一個解決辦法來設置單選按鈕以及其間
0

,在這個例子中,我使用一個列表框,但可以使用任何其它數據。

下面是完整的代碼:(對測試電子表格中的集裝箱)

function radiotest() { 
    var app = UiApp.createApplication(); 
    var panel = app.createVerticalPanel(); 
    var radioValue = app.createTextBox().setId('radioValue'); 
     radioValue.setId("radioValue").setName("radioValue"); 
    var listhandler = app.createServerHandler('listhandler').addCallbackElement(panel); 
    var list = app.createListBox().addChangeHandler(listhandler).setName('list');  
    for(var i = 1; i < 10; i++){ 
    var name = 'choice '+i; 
    list.addItem('Activate '+name,name) 
    var handler = app.createClientHandler().forTargets(radioValue).setText(name); 
    panel.add(app.createRadioButton('radioButtonGroup',name).addValueChangeHandler(handler).setId(name)); 
    } 
    panel.add(radioValue); 
    var getit=app.createButton("Valide").setId("val"); 
    panel.add(getit).add(list)     
    var handler = app.createServerHandler("valide") 
    handler.addCallbackElement(panel) 
    getit.addClickHandler(handler); 
    app.add(panel); 
SpreadsheetApp.getActiveSpreadsheet().show(app);// show app 
} 
// 
function valide(e){ ;// This function is called when key "validate" is pressed 
    var sh = SpreadsheetApp.getActiveSheet(); 
    var RadioButton = e.parameter.radioValue;    
    sh.getRange('A1').setValue(RadioButton); 
    var app = UiApp.getActiveApplication(); 
    return app; 
}​ 

function listhandler(e){ ;// This function is called when listBox is changed 
    var sh = SpreadsheetApp.getActiveSheet(); 
    var app = UiApp.getActiveApplication(); 
    var listvalue = e.parameter.list 
    var radioValue = app.getElementById('radioValue').setValue(listvalue) 
    sh.getRange('A2').setValue(listvalue); 
    var radiobutton = app.getElementById(listvalue) 
    radiobutton.setValue(true) 
return app; 
}​ 

所選RadioButton值來在文本框的值和列表框允許選擇其中的單選按鈕被激活...它顯示了這樣

enter image description here

也有另一種方法,通過eddyparkinson是使用e.parameter.source規定,但這個工程如果處理程序直接分配給radioButton而不使用「提交」按鈕。在很多情況下,它可以被使用並使得代碼稍微輕一些。 這是對此代碼的測試

function radiotest2() { 
    var app = UiApp.createApplication(); 
    var panel = app.createVerticalPanel(); 
    var listhandler = app.createServerHandler('listhandler2').addCallbackElement(panel); 
    var list = app.createListBox().addChangeHandler(listhandler).setName('list');  
    var handler = app.createServerHandler("valide2") 
    handler.addCallbackElement(panel) 
    for(var i = 1; i < 10; i++){ 
    var name = 'choice '+i; 
    list.addItem('Activate '+name,name) 
    panel.add(app.createRadioButton('radioButtonGroup',name).setId(name).addClickHandler(handler)); 
    } 
    panel.add(list)     
    app.add(panel); 
SpreadsheetApp.getActiveSpreadsheet().show(app);// show app 
} 


function valide2(e){ ;// This function is called when a radioButton is selected 
    var sh = SpreadsheetApp.getActiveSheet(); 
    var source = e.parameter.source; 
    var radioValue = ''; 
    if(source.match('choice')=='choice'){radioValue=source} 
    sh.getRange('A1').setValue(radioValue); 
    var app = UiApp.getActiveApplication(); 
    return app; 
}​ 

function listhandler2(e){ ;// This function is called when listBox is changed 
    var sh = SpreadsheetApp.getActiveSheet(); 
    var app = UiApp.getActiveApplication(); 
    var listvalue = e.parameter.list 
    sh.getRange('A2').setValue(listvalue); 
    var radiobutton = app.getElementById(listvalue) 
    radiobutton.setValue(true) 
return app; 
}​ 
相關問題