Copy link to clipboard
Copied
Hello everyone! As always I have here a small and theoretically easy curiosity about Panels with Radio buttons.
How to check and alert by stating that none of the radio options have been checked when I click the ok button!
How to check and alert, stating that none of the radio options was enabled when I click the ok button!
Thank you!
var w = new Window("dialog", "teste Radio");
w.orientation = "row";
w.alignChildren = "top"; // insert this line
var radio_group = w.add('panel', undefined, "Radio");
radio_group.alignChildren = "left";
radio_group.add("radiobutton", undefined, "Radio 1");
radio_group.add("radiobutton", undefined, "Radio 2");
var btnGroup = w.add("group");
btnGroup.orientation = "column";
btnGroup.add('button', { x: 90, y: 125, width: 90, height: 25 }, 'Ok', { name: 'ok' });
btnGroup.add('button', { x: 240,y: 125, width: 90, height: 25 }, 'Cancel', {name: 'cancel'});
//radio_group.children[0].value = true; //
function selected_rbutton(rbuttons) {
for (var i = 0; i < rbuttons.children.length; i++) {
if (rbuttons.children.value == true) {
return rbuttons.children.text;
}
}
}
// Scrips.jsx
if (w.show() == 1) {
var chosenRadioButton = selected_rbutton(radio_group);
switch (chosenRadioButton) {
// Scrips Radio 1
case "Radio 1":
alert("This is the Radio 1 script");
break;
// Scrips Radio 2
case "Radio 2":
alert("This is the Radio 2 script");
break;
}
}
Small tweak to your original code:
...var w = new Window("dialog", "teste Radio");
w.orientation = "row";
w.alignChildren = "top"; // insert this line
var radio_group = w.add('panel', undefined, "Radio");
radio_group.alignChildren = "left";
radio_group.add("radiobutton", undefined, "Radio 1");
radio_group.add("radiobutton", undefined, "Radio 2");
var btnGroup = w.add("group");
btnGroup.ori
Copy link to clipboard
Copied
Small tweak to your original code:
var w = new Window("dialog", "teste Radio");
w.orientation = "row";
w.alignChildren = "top"; // insert this line
var radio_group = w.add('panel', undefined, "Radio");
radio_group.alignChildren = "left";
radio_group.add("radiobutton", undefined, "Radio 1");
radio_group.add("radiobutton", undefined, "Radio 2");
var btnGroup = w.add("group");
btnGroup.orientation = "column";
btnGroup.add('button', { x: 90, y: 125, width: 90, height: 25 }, 'Ok', { name: 'ok' });
btnGroup.add('button', { x: 240,y: 125, width: 90, height: 25 }, 'Cancel', {name: 'cancel'});
//radio_group.children[0].value = true; //
function selected_rbutton(rbuttons) {
for (var i = 0; i < rbuttons.children.length; i++) {
if (rbuttons.children.value == true) {
return rbuttons.children.text;
}
}
return "none"
}
// Scrips.jsx
if (w.show() == 1) {
var chosenRadioButton = selected_rbutton(radio_group);
switch (chosenRadioButton) {
// Scrips Radio 1
case "Radio 1":
alert("This is the Radio 1 script");
break;
// Scrips Radio 2
case "Radio 2":
alert("This is the Radio 2 script");
break;
default:
alert("no radio button has been chosen")
}
}
Davide
Copy link to clipboard
Copied
This is one way.
var w = new Window("dialog", "teste Radio");
w.orientation = "row";
w.alignChildren = "top";
var radio_group = w.add('panel', undefined, "Radio");
radio_group.alignChildren = "left";
radio_group.add("radiobutton", undefined, "Radio 1");
radio_group.add("radiobutton", undefined, "Radio 2");
radio_group.add("radiobutton", undefined, "Radio 3");
radio_group.add("radiobutton", undefined, "Radio 4");
var btnGroup = w.add("group");
btnGroup.orientation = "column";
btnGroup.add('button', { x: 90, y: 125, width: 90, height: 25 }, 'Ok', { name: 'ok' });
btnGroup.add('button', { x: 240,y: 125, width: 90, height: 25 }, 'Cancel', {name: 'cancel'});
//radio_group.children[0].value = true; //
function selected_rbutton(rbuttons) {
for (var i = 0; i < rbuttons.children.length; i++) {
if (rbuttons.children.value == true)
return alert("This is the " + rbuttons.children.text + " script");
}
alert("No radio button selected");
}
if (w.show() == 1) selected_rbutton(radio_group);
Copy link to clipboard
Copied
Another possibility is to work with events:
var w = new Window("dialog", "teste Radio");
var theSel;
w.orientation = "row";
w.alignChildren = "top"; // insert this line
var radio_group = w.add('panel', undefined, "Radio");
radio_group.alignChildren = "left";
var r1 = radio_group.add("radiobutton", undefined, "Radio 1");
var r2 = radio_group.add("radiobutton", undefined, "Radio 2");
var btnGroup = w.add("group");
btnGroup.orientation = "column";
var okBtn = btnGroup.add('button', { x: 90, y: 125, width: 90, height: 25 }, 'Ok', { name: 'ok' });
btnGroup.add('button', { x: 240,y: 125, width: 90, height: 25 }, 'Cancel', {name: 'cancel'});
btnGroup.addEventListener ("click", function(evt){
var theText;
if ( r1.value ) theText = r1.text;
else if ( r1.value ) theText = r1.text;
theSel = theText;
});
okBtn.onClick = function() {
if ( !theSel ) {
alert("You need to select a RadioButton please…" );
return;
}
w.close(1);
}
if (w.show() == 1) {
alert( theSel );
}
Copy link to clipboard
Copied
Both work perfectly well! Davide_Barranca,SuperMerlin and Loic.Aigon thanks for the support.