使用radioButton()时的奇怪行为 [英] Strange behaviour when using radioButton()

查看:44
本文介绍了使用radioButton()时的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码让我有些头疼.这是一个非常简单的应用程序,具有2个单选按钮和一个按钮.这个应用程序的想法是在电子表格中记录所选单选按钮的值.现在,如果我选择第一个单选按钮(一个"),然后更改为第二个单选按钮(两个"),然后又回到一个",然后单击记录"按钮,则该应用将在电子表格.这种积累选择.

The code below is giving me some headache. It's a very simple application that has 2 radio buttons and a button. The idea of this app is to record in the spreadsheet the value of the radio button selected. Now, if I select the 1st radio button("one") and then change to the second radio button("two") and then back to "one" and click the "Record" button, the app will record 3 times in the spreadsheet. It kind of accumulates the selections.

不知道我是否清楚自己,但是这里是模拟该问题的步骤:

Don't know if I made myself clear, but here are the steps to simulate the issue:

1-运行下面的应用程序;2-选择单选按钮一个",然后选择两个"和一个",然后按记录"按钮;3-检查电子表格中是否有重复的值.

1- Run the application below; 2- Select radio button "one", then "two" and the "one" again and press the "Record" button; 3- Check the spreadsheet for duplicated values.

按住单选按钮的次数越多,记录的重复值就越多.

The more you keep clicking the radio buttons, the more duplicated values will be recorded.

任何想法为什么会发生这种情况以及如何解决这个问题?

Any idea why this happens and how can I overcome this issue?

function myAppFunction() {
var mydoc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication().setTitle('Here is the title bar'); 
var panel = app.createVerticalPanel().setId('panel');

//using setName will give you the ability to get the value  
var myRadio1 = app.createRadioButton("group","one").setName('myRadio1').setId('myRadio1');
var myRadio2 = app.createRadioButton("group","two").setName('myRadio2').setId('myRadio2');
var button = app.createButton("Gravar").setId("record_");
//A label will be used to give feedback on the value of the checkBox 
var infoLabel = app.createLabel('Check the box and click submit').setId('infoLabel');

//handlers 
var handler = app.createServerChangeHandler('radio1');
handler.addCallbackElement(panel);  
myRadio1.addClickHandler(handler);

var handler2 = app.createServerChangeHandler('radio2');
handler2.addCallbackElement(panel);  
myRadio2.addClickHandler(handler2);


//put everything in the UI 
panel.add(myRadio1);      
panel.add(myRadio2); 
panel.add(button);
panel.add(infoLabel);

app.add(panel);  
mydoc.show(app);  
}
function radio1(e){
var app = UiApp.getActiveApplication(); 
app.getElementById('myRadio2').setValue(false);
app.getElementById('infoLabel').setText('one is: ' + e.parameter.myRadio1);
var panel = app.getElementById("panel");
var button = app.getElementById("gravar");
var handler = app.createServerClickHandler("record_");
handler.addCallbackElement(panel);
button.addClickHandler(handler);
return app;
}

function radio2(e){
var app = UiApp.getActiveApplication(); 
app.getElementById('myRadio1').setValue(false);
app.getElementById('infoLabel').setText('two is: ' + e.parameter.myRadio2); 
var button = app.getElementById("gravar");
var handler = app.createServerClickHandler("gravar_");
handler.addCallbackElement(app.getElementById("panel"));
button.addClickHandler(handler);

return app;
}
function record_(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var lastRow = sheet.getLastRow();
var freeCell = sheet.getRange("A1").offset(lastRow, 0);
freeCell.setValue(e.parameter.myRadio1);
}

如果您引用

推荐答案

单选按钮必须具有相同的名称,才能在普通的异或模式下工作.com/apps-script/class_radiobutton"rel =" nofollow> doc 您会注意到,在标准面板中使用时,这是必需的.这是一个简单的示例:

Radio buttons must have the same name to work in normal exclusive OR mode, if you refer to the doc you'll notice that this is required when using in a standard panel. Here is a simple example :

function radiotest() {
  var app = UiApp.createApplication();
  var panel = app.createVerticalPanel();
  var radioValue = app.createTextBox();
      radioValue.setId("radioValue").setName("radioValue").setVisible(false);
  for(var i = 1; i < 10; i++){
    var name = 'choice '+i;
    var handler = app.createClientHandler().forTargets(radioValue).setText(name);
    panel.add(app.createRadioButton('radio',name).addValueChangeHandler(handler));
  }
  panel.add(radioValue);
  var getit=app.createButton("Valide").setId("val");
  panel.add(getit)                   
  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 ss = SpreadsheetApp.getActiveSpreadsheet();
  var RadioButton = e.parameter.radioValue;               
  sh.getRange('A1').setValue(RadioButton);
  var app = UiApp.getActiveApplication();
  return app;
}​

这篇关于使用radioButton()时的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆