You are viewing limited content. For full access, please sign in.

Question

Question

Hide the submit button base on the answers to two radio buttons

asked on April 12, 2019

I am completely new to Laserfiche, JavaScript, and forms. I have a form with two questions on it that if the answer to either one of them is "yes", the form can't be submitted. Most of the form is hidden unless they answer "no", but forms is putting a submit button on the page. How can I hide the submit button unless they have answered "no" to both questions.

0 0

Replies

replied on April 12, 2019 Show version history

If you hide the built-in submit button with CSS

.Submit {
    display:none !important;
}

Then you can use a CustomHTML element to build your own Submit button

<button class="customBtn" type="Submit">Submit</button>

This custom button with the Submit type will still trigger all the form validation and everything, so you don't have to do much customization.

Next, you just use a Field Rule to show/hide your custom button based on the values of your radio inputs.

You'll probably need some CSS to style it exactly how you want (that's what the customBtn class is for), but no JavaScript required!

1 0
replied on January 18, 2023

Hi Jason,

 

I have a similar issue with my form and when I was looking for answers here I found your comment. I'm trying to do the same thing with two checkboxes. If two different checkboxes,checked with certain 2 values, the form needs to hide the submit button.
 

Now I tried using building my own submit button but the issue is the form have page breaks and as you know, default submit button always appear at the last page.  My form have 7 pages. Should I need to create 7 different submit buttons for each page then manipulate them with field rules? 

I'm not really familiar javascript. I tried to reverse engineer what I read here and wrote the code below but couldn't get it worked. I'd really appreciate any help. 

Thank you!

 

$(document).ready(function () {
    $('.Submit').show();
 $('#q71 input' && '#q11 input').change(function(){
     if($('#q71 input:checkbox[value=Client Name]') && $('#q11 input:checkbox[value=Imports]')){
         $('.Submit').hide();
    
     }
else {
    $('.Submit').show();
}
 })
 });

0 0
replied on January 18, 2023

Onur,

You don't need custom JavaScript. You can still use Field Rules.

You just need the right condition, which in your case sounds like it would be a "When All" condition with the two checkboxes you want.

For example,

As for the pagination part, you don't need to add a custom button to every single page of the form.

You only need the button on whatever will/could be the last page (in some cases the last page can change if you have field rules showing/hiding pages in which case you'd need some extra rules to make sure it only appears on the "current" last page).

I tend to add custom buttons for page navigation to all my pages anyway because I like to force validation for each page before allowing users to move onto the next page, but that's not totally necessary.

The only "tricky" part when you involve pagination is the button placement since you'll have the "Previous" button at the bottom, so if you want it to be in the same place as the default button you'd have to figure out the right CSS or hide all the buttons on that page and add your own "Previous" button to go along with the custom Submit button.

A custom nav button requires a little bit of JavaScript since "previous" and "next" are not html button types, but it's not terribly complicated.

$('.custom-next').on('click', function () {
    $('.cf-page.active .cf-next-btn').click();
});

$('.custom-prev').on('click', function () {
    $('.cf-page.active .cf-prev-btn').click();
});

Basically, this just makes it so when you click your "custom" nav buttons, it just triggers a click event on the associated button for the active (visible) page.

1 0
replied on January 19, 2023

Thank you for your answer.

 

The pages appear dissappear with different field rules and there are sections on each page appear and dissappear there too but I think I can add another section for custom nav buttons. I'll try to make it work. Other than that, would you able to tell me what I'm I missing on the code I shared for future references please?  It passes the validation for syntax but not doing whats intended.

 

Thanks

 

 

 

 

0 0
replied on January 19, 2023

It's hard to say what the issue might be without more information. Are you getting any JavaScript errors in the console?

Try adding alert("1") or console.log("1") in various parts of the code with different messages to see which parts actually fire.

0 0
replied on January 19, 2023

I figured it out after, it was something stupid. The user checks "Client Name" from checkbox but the value is "Client_Name"  in the backend with underscore between words. I changed it and the code worked.

Thank you for all your help Jason I appreciate it. I'll be using the custom buttons from now on, its much more simpler than writing the code!

0 0
replied on April 13, 2019 Show version history

I do this via Javascript on a lot of my forms.

Here's some Javascript you can add that will work with any number of Radio Buttons (I tested using 3 of them).  Make sure your radio buttons are set to include values, and that your desired value is No.  Give all of your radio buttons the CSS Class of mustBeNo.  Then add the following Javascript.

When the form loads, and when any of the mustBeNo radio buttons is changed, it checks to make sure every one of the mustBeNo radio buttons has been marked as No.  If any of them are not No, it hides the Submit button.  If all of them are set to No, then it will show the Submit button.

$(document).ready(function () {
  
  //Run the showHideSubmit function when the form loads
  showHideSubmit();
  
  //Run the showHideSubmit function whenever a radio button changes
  $('.mustBeNo').change(function(){
    showHideSubmit();
  });
  
  //Check for all mustBeNo radio buttons to have a value of No,
  //Hide the submit button if any of them do not have a value of No
  //Show the submit button is they all have a value of no
  function showHideSubmit() { 
    var n = 0;
    $('.mustBeNo input[value=No]').each(function() { 
      if (!$(this).prop('checked')) { 
        n++;
      }
    });
    if (n == 0) { 
      $('.Submit').show();
    }
    else { 
      $('.Submit').hide();
    }
  }
  
});

 

1 0
replied on April 15, 2019

It worked perfectly. Thank you!

0 0
replied on April 15, 2019

@████████ - Please consider marking the response that you ended up using as the answer to your question.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.