CMS Development

Alex_
トップ投稿者

Disabling Multiple Form Fields When Checkbox is Checked

解決

Hi All,

 

I can't, for the life of me, figure this part of my form out. I have 3 fields that I want to be disabled when a specific box is checked and I haven't had any luck with any docs or community posts being able to solve this issue. Can anyone help with this?

0 いいね!
2件の承認済みベストアンサー
Jake_Lett
解決策
ガイド役 | Solutions Partner
ガイド役 | Solutions Partner

Disabling Multiple Form Fields When Checkbox is Checked

解決

Hi Alex. Good question. The listeners are important because it gives you confidence the form is on the page. Sometimes things load faster than others and you can run code dependent on something that is not fully loaded in the page. So you get bugs where it works sometimes and breaks other times.

 

If you are new to javascript, make sure you learn the difference between document ready, on window load, dom inserted, timeout, and callbacks. All of these things determine when  your javascript will file and you will often want control on when that happens.

元の投稿で解決策を見る

Alex_
解決策
トップ投稿者

Disabling Multiple Form Fields When Checkbox is Checked

解決

Got the form working a few weeks ago and wanted to share the code for others wanting to do the same thing:

 

$(document).on('change', 'input[name="field_name"]', function() {
  if($('input[name="field_name"]').prop('checked')){
    $('input[name="field_name"]').attr('disabled', true);
    $('input[name="field_name"]').attr('disabled', true);
    $('input[name="field_name"]').attr('disabled', true);
    $('.hs_field_class').css('opacity', 0.6);
    $('.hs_field_class').css('opacity', 0.6);
    $('.hs_field_class').css('opacity', 0.6);
    $('input[type="radio"]').prop('checked', false);
  } else {
    $('input[name="field_name"]').attr('disabled', false);
    $('input[name="field_name"]').attr('disabled', false);
    $('input[name="field_name"]').attr('disabled', false);
    $('.hs_field_class').css('opacity', 1.0);
    $('.hs_field_class').css('opacity', 1.0);
    $('.hs_field_class').css('opacity', 1.0);
  }
});

元の投稿で解決策を見る

12件の返信
SB21
参加者

Disabling Multiple Form Fields When Checkbox is Checked

解決

HI,

I got a query on hubspot Landing page. I have got 5 check boxes in a landing page. If i check the last one all other opted must be unchecked and and if any of the first 4 is checked the last one must be un checked. 

0 いいね!
dennisedson
HubSpot製品開発チーム
HubSpot製品開発チーム

Disabling Multiple Form Fields When Checkbox is Checked

解決

@SB21 , what is the issue?

0 いいね!
SB21
参加者

Disabling Multiple Form Fields When Checkbox is Checked

解決

HI

 I am new to hubspot and i have got a query in landing pages.

I have got 5 check boxes in a landing page. If i check the last checkbox all other opted must be unchecked and if any of the first 4 is checked the last one must be un checked. 

 

EX: Like a Preference form. If thy click optout all their subscriptions must be unchecked and vice versa.

 

SB21_0-1623331936844.png

 

0 いいね!
dennisedson
HubSpot製品開発チーム
HubSpot製品開発チーム

Disabling Multiple Form Fields When Checkbox is Checked

解決

@Alex_ should be applicable to your use case.  You will need to replace the the name to be whatever the name of your input field is, though.

0 いいね!
SB21
参加者

Disabling Multiple Form Fields When Checkbox is Checked

解決

Thanks. Do we need to add that at Additional code snippets of Landing page @alex @dennisedson . I tried by adding it there but it dint work.

0 いいね!
dennisedson
HubSpot製品開発チーム
HubSpot製品開発チーム

Disabling Multiple Form Fields When Checkbox is Checked

解決

@SB21 , you would need to add it to your module js.  That code requires jquery so make sure that is also loaded. 

You should also consider posting a link to your page with an example of this as well as the exact code you are using

0 いいね!
SB21
参加者

Disabling Multiple Form Fields When Checkbox is Checked

解決

Can we schedule form submissions to deliver to the inbox directly on a daily basis in hubspot ?

0 いいね!
Alex_
解決策
トップ投稿者

Disabling Multiple Form Fields When Checkbox is Checked

解決

Got the form working a few weeks ago and wanted to share the code for others wanting to do the same thing:

 

$(document).on('change', 'input[name="field_name"]', function() {
  if($('input[name="field_name"]').prop('checked')){
    $('input[name="field_name"]').attr('disabled', true);
    $('input[name="field_name"]').attr('disabled', true);
    $('input[name="field_name"]').attr('disabled', true);
    $('.hs_field_class').css('opacity', 0.6);
    $('.hs_field_class').css('opacity', 0.6);
    $('.hs_field_class').css('opacity', 0.6);
    $('input[type="radio"]').prop('checked', false);
  } else {
    $('input[name="field_name"]').attr('disabled', false);
    $('input[name="field_name"]').attr('disabled', false);
    $('input[name="field_name"]').attr('disabled', false);
    $('.hs_field_class').css('opacity', 1.0);
    $('.hs_field_class').css('opacity', 1.0);
    $('.hs_field_class').css('opacity', 1.0);
  }
});
nlafakis
トップ投稿者 | Elite Partner
トップ投稿者 | Elite Partner

Disabling Multiple Form Fields When Checkbox is Checked

解決

Hey Alex!,

 

Do you happen to have a jsfiddle of this, or maybe a codepen example? I've tried this code in my form and replaced the proper parts of it, but it breaks the form and doesn't dispay anything.

 

Edit:

 

Here's the fiddle I'm working with that has a test form in it - Locking the checkbox of a HS form - JSFiddle - Code Playground

0 いいね!
Jake_Lett
ガイド役 | Solutions Partner
ガイド役 | Solutions Partner

Disabling Multiple Form Fields When Checkbox is Checked

解決

This would require some custom javascript after the form is loaded. 

 

Reference on writing a custom script behavior  - https://stackoverflow.com/questions/22763965/jquery-to-disable-checkbox-if-another-is-checked

 

load script after the form has loaded - https://legacydocs.hubspot.com/global-form-events


Jacob Lett

Freelance HubSpot CMS Developer & Web Designer


bootstrapcreative-horizontal-trim.png

Located in Michigan, USA

Get a free estimate now 

Alex_
トップ投稿者

Disabling Multiple Form Fields When Checkbox is Checked

解決

Hey @Jake_Lett, thanks for replying. I'm confused; will the script not work without event listeners? I'm still learning js and consider myself at the very, very begginer stage.

0 いいね!
Jake_Lett
解決策
ガイド役 | Solutions Partner
ガイド役 | Solutions Partner

Disabling Multiple Form Fields When Checkbox is Checked

解決

Hi Alex. Good question. The listeners are important because it gives you confidence the form is on the page. Sometimes things load faster than others and you can run code dependent on something that is not fully loaded in the page. So you get bugs where it works sometimes and breaks other times.

 

If you are new to javascript, make sure you learn the difference between document ready, on window load, dom inserted, timeout, and callbacks. All of these things determine when  your javascript will file and you will often want control on when that happens.