首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在页面加载和无线电输入单击上运行jQuery函数

在页面加载和无线电输入单击上运行jQuery函数
EN

Stack Overflow用户
提问于 2020-07-22 11:14:54
回答 2查看 52关注 0票数 0

感觉有点傻,我似乎不能百分之百地工作.

当选择单选按钮时,下面的代码工作得很好,但是我也需要它在页面加载上运行--似乎找不出如何让它发挥作用。这里的(很多)帖子似乎没有提供解决方案--我很有信心我错过了一些非常简单的东西!(你可能已经猜到了,JS不是我的强项!)

代码语言:javascript
复制
$('#cvl_mb_services .content-switch').on('click', 'input[type="radio"]', function(){

    var parent      = $(this).parent().parent().parent().parent().parent().parent().attr("id");
    var inputValue  = $(this).closest('input[type="radio"]').attr("value");
    var targetBox   = '#' + parent + ' .' + inputValue + '-fields';
  
    $(targetBox).removeClass('hide-field');
  
    // console.log(parent + ' ' + inputValue + ' ' + targetBox);

});

Html标记如下。(需要注意的是:可以有几个.box容器,而且我对html没有太多的直接控制,因为它是由插件输出的)

代码语言:javascript
复制
<div id="cvl_mb_services">
  <div id="box_01" class="box">
    <div class="content-switch">
      <ul>
        <li><input type="radio" class="content-option" value="header" checked="checked"><label>Header</label></li>
        <li><input type="radio" class="content-option" value="content"><label>Content</label></li>
        <li><input type="radio" class="content-option" value="footer"><label>Footer</label></li>
      </ul>
    </div>
    <div class="fields header-fields hide-field">
      <p>You should only see this content of the Header Option is selected (or pre-selected) in this box</p>
    </div>
    <div class="fields content-fields hide-field">
      <p>You should only see this content of the Content Option is selected (or pre-selected) in this box</p>
    </div>
    <div class="fields footer-fields hide-field">
      <p>You should only see this content of the Footer Option is selected (or pre-selected) in this box</p>
    </div>
  </div><!-- #box_01 -->
  <div id="box_02" class="box">
    <div class="content-switch">
      <ul>
        <li><input type="radio" class="content-option" value="header"><label>Header</label></li>
        <li><input type="radio" class="content-option" value="content" checked="checked"><label>Content</label></li>
        <li><input type="radio" class="content-option" value="footer"><label>Footer</label></li>
      </ul>
    </div>
    <div class="fields header-fields hide-field">
      <p>You should only see this content of the Header Option is selected (or pre-selected) in this box</p>
    </div>
    <div class="fields content-fields hide-field">
      <p>You should only see this content of the Content Option is selected (or pre-selected) in this box</p>
    </div>
    <div class="fields footer-fields hide-field">
      <p>You should only see this content of the Footer Option is selected (or pre-selected) in this box</p>
    </div>
  </div><!-- #box_02 -->
</div>

提前感谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-07-22 13:17:25

现在用以下方法工作..。

代码语言:javascript
复制
$( "#cvl_mb_services .content-switch" ).each(function(index, el) {
    var parent        = $(el).parent().parent().attr("id");
    var inputValue    = $('#' + parent + ' input[type=radio]:checked').val();
    var targetBox   = '#' + parent + ' .cvl-' + inputValue + '-fields';

    $(targetBox).removeClass('cvl-hide');
});


$('#cvl_mb_services .content-switch').on('click', 'input[type="radio"]', function(){

    var parent      = $(this).parent().parent().parent().parent().parent().parent().attr("id");
    var inputValue  = $(this).closest('input[type="radio"]').attr("value");
    var targetBox   = '#' + parent + ' .cvl-' + inputValue + '-fields';

    if (inputValue == 'content') {
        $('#' + parent + ' .cvl-content-fields').removeClass('cvl-hide');
        $('#' + parent + ' .cvl-header-fields').addClass('cvl-hide');
        $('#' + parent + ' .cvl-footer-fields').addClass('cvl-hide');
    } else if (inputValue == 'header') {
        $('#' + parent + ' .cvl-content-fields').addClass('cvl-hide');
        $('#' + parent + ' .cvl-header-fields').removeClass('cvl-hide');
        $('#' + parent + ' .cvl-footer-fields').addClass('cvl-hide');
    } else if (inputValue == 'footer') {
        $('#' + parent + ' .cvl-content-fields').addClass('cvl-hide');
        $('#' + parent + ' .cvl-header-fields').addClass('cvl-hide');
        $('#' + parent + ' .cvl-footer-fields').removeClass('cvl-hide');
    }

});

我现在想让这个变得更干/更有效率,并在此张贴.

Suggestions to make this jQuery function more DRY / more efficient

票数 0
EN

Stack Overflow用户

发布于 2020-07-22 11:47:18

我会将回调代码放入函数中,并在触发文档就绪事件时调用它,然后将其称为单选按钮的回调函数,单击事件侦听器。

代码语言:javascript
复制
function handleServicesRadioButton(elem) {
    var parent      = elem.parent().parent().parent().parent().parent().parent().attr("id");
    var inputValue  = elem.closest('input[type="radio"]').attr("value");
    var targetBox   = '#' + parent + ' .' + inputValue + '-fields';
    $(targetBox).removeClass('hide-field');
}

$(document).ready(function() {
    var radioButtonElem = $(".box .content-option").eq(0); //here I am selecting the first radio button for example
    handleServicesRadioButton(radioButtonElem); //called on page load
    servicesElem.on('click', 'input[type="radio"]', function(){
        var elem = $(this);
        handleServicesRadioButton(elem); //called on radio button click
    });
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63032930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档