简体   繁体   English

如何设置元素值并在加载 Javascript 时运行 function

[英]How to set element value and run function on load Javascript

I'm trying to refactor a small Javascript function that simply adds an active class to the parent <div> of a series of radio buttons on page load as well as via a change.我正在尝试重构一个小的 Javascript function ,它只是将一个active的 class 添加到页面加载时一系列单选按钮的父<div>以及更改。

My function is:我的 function 是:

function toggleActiveState (event) {
  const el = event.target
  const parent = el.closest('[data-toggle-has-btn-group]')
  const buttons = parent.getElementsByClassName('js__btn-toggle')

  var i
  for (i = 0; i < buttons.length; i++) {
    buttons[i].classList.remove('active')
  }

  el.parentElement.classList.add('active')
}

My markup is:我的标记是:

<div class="form-group">
  <div class="btn-group-toggle d-flex flex-row" data-toggle="buttons" data-toggle-has-btn-group>
    <label class="btn btn-custom btn-outline-primary btn-sm active flex-fill mr-1 js__btn-toggle">
      <input type="radio" name="options" id="option1" value="Mr" checked onclick="toggleActiveState(event)"> Mr
    </label>
    <label class="btn btn-custom btn-outline-primary btn-sm flex-fill mx-1 js__btn-toggle">
      <input type="radio" name="options" id="option2" value="Mrs" onclick="toggleActiveState(event)"> Mrs
    </label>
    <label class="btn btn-custom btn-outline-primary btn-sm flex-fill mx-1 js__btn-toggle">
      <input type="radio" name="options" id="option3" value="Miss" onclick="toggleActiveState(event)"> Miss
    </label>
    <label class="btn btn-custom btn-outline-primary btn-sm flex-fill ml-1 js__btn-toggle">
      <input type="radio" name="options" id="option4" value="Other" onclick="toggleActiveState(event)"> Other
    </label>
  </div>
</div>

When clicking a radio button, the toggleActiveState(event) runs and I'm able to remove the active classes and add a class to the input I clicked.单击单选按钮时, toggleActiveState(event)运行,我可以删除活动类并将 class 添加到我单击的输入中。

I have multiple of these .btn-group-toggle elements on the page.我在页面上有多个这些.btn-group-toggle元素。

What I need to do is be able to set the active class state of say the Mrs element on page load and it's value by calling my function like so:我需要做的是能够设置活动的 class state 说页面加载时的Mrs元素及其值通过调用我的 function 像这样:

toggleActiveState('options', 'Mrs') and then this would be active, and selected... toggleActiveState('options', 'Mrs')然后这将是活动的,并被选中...

Any ideas?有任何想法吗?

You can add event listener on window load and add the class there.您可以在 window 负载上添加事件侦听器,并在那里添加 class。 Like I've done below based on your code.就像我根据您的代码在下面所做的那样。

 function toggleActiveState (event) { const el = event.target const parent = el.closest('[data-toggle-has-btn-group]') const buttons = parent.getElementsByClassName('js__btn-toggle') var i for (i = 0; i < buttons.length; i++) { buttons[i].classList.remove('active') } el.parentElement.classList.add('active') } window.addEventListener('load', () => { const checkedInput = document.querySelector('input[type="radio"]:checked'); checkedInput.parentElement.classList.add('active'); })
 .active { color: red; }
 <div class="form-group"> <div class="btn-group-toggle d-flex flex-row" data-toggle="buttons" data-toggle-has-btn-group> <label class="btn btn-custom btn-outline-primary btn-sm flex-fill mr-1 js__btn-toggle"> <input type="radio" name="options" id="option1" value="Mr" checked onclick="toggleActiveState(event)"> Mr </label> <label class="btn btn-custom btn-outline-primary btn-sm flex-fill mx-1 js__btn-toggle"> <input type="radio" name="options" id="option2" value="Mrs" onclick="toggleActiveState(event)"> Mrs </label> <label class="btn btn-custom btn-outline-primary btn-sm flex-fill mx-1 js__btn-toggle"> <input type="radio" name="options" id="option3" value="Miss" onclick="toggleActiveState(event)"> Miss </label> <label class="btn btn-custom btn-outline-primary btn-sm flex-fill ml-1 js__btn-toggle"> <input type="radio" name="options" id="option4" value="Other" onclick="toggleActiveState(event)"> Other </label> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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