简体   繁体   中英

condition on radio button onclick, if false don't check

onclick of one radio button i have to check one condition if true i have to check it or restore to same old status,

html

<input type="radio" name="test" id="radio0" onclick="myFunction()" checked /> 
<input type="radio" name="test" id="radio1" onclick="myFunction()" /> 
<input type="radio" name="test" id="radio2" onclick="myFunction()" /> 

JS

globalCondition = false;
function myFunction()
{
   if(globalCondition)
   {
      //some codes and it will check clicked radio button anyway 
   }
   else
   {
       return false; // i tried this but its not working
      /*here i don't want to check clicked radio button, and previously
       checked button should be checked*/ 
   }
}

try this:

globalCondition = false;
function myFunction(e)
{
   if(globalCondition)
   {
      //some codes and it will check clicked radio button anyway 
   }
   else
   {
       e.preventDefault();
       return false; // i tried this but its not working
      /*here i don't want to check clicked radio button, and previously
       checked button should be checked*/ 
   }
}

USe like this

<input type="radio" name="test" id="radio1" onclick="return myFunction()" /> 

javascript

globalCondition = false;
function myFunction(e)
{
   if(globalCondition)
   {
      //some codes and it will check clicked radio button anyway 
     return true;
   }
   else
   {

       return false; // i tried this but its not working
      /*here i don't want to check clicked radio button, and previously
       checked button should be checked*/ 
   }
}

As I said in comment return in the function will not do anything as you're not returning the function value in the in-line code.

Although the other solutions offered are correct, to keep your code unobtrusive , you should not have inline JS at all (remove the onclick= 's).

I realize the question was not tagged jQuery , but maybe it should have been: Instead on onclick you should use a jQuery event handler, selecting only the set of radio buttons.

JSFiddle: http://jsfiddle.net/TrueBlueAussie/KVwL3/1/

globalCondition = false;

$(function(){
    $("[name=test]").click(function(e){
       if(globalCondition)
       {
          //some codes and it will check clicked radio button anyway 
       }
       else
       {
           return false;
            // or
            e.preventDefault();
           /*here i don't want to check clicked radio button, and previously
           checked button should be checked*/ 
       }
    });
});

Notes:

DOM ready event:

$(function(){ YOUR CODE HERE }); is a shortcut for $(document).ready(function(){ YOUR CODE HERE});

Selectors

If an attribute [] selector = value contains special characters it needs to be quoted:

eg

$('[name="IstDynamicModel[SD_WO_CREATE]"]')

There are any number of selectors that will choose just the three radio buttons. As this is a simple one-off connection, at startup, there is no real speed difference between any options, but you would normally try and make the selector specific in ways that might make use of various lookup tables available to the browser:

eg

$('input[type=radio][name="IstDynamicModel[SD_WO_CREATE]"]')

This will be slightly faster as it will reduce the slowest check (the name= ) to only radio input s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM