Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Check Radio Options!

Contributor ,
Dec 19, 2017 Dec 19, 2017

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;   

}

}

TOPICS
Actions and scripting
954
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 19, 2017 Dec 19, 2017

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

...
Translate
Adobe
Community Expert ,
Dec 19, 2017 Dec 19, 2017

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

Davide Barranca - PS developer and author
www.ps-scripting.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Dec 19, 2017 Dec 19, 2017

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);     

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Dec 19, 2017 Dec 19, 2017

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 );

Capture d’écran 2017-12-19 à 16.17.26.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 19, 2017 Dec 19, 2017
LATEST

Both work perfectly well! Davide_Barranca,SuperMerlin   and Loic.Aigon thanks for the support.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines