如果/ Else语句不正常(Else语句特别胜出)

时间:2016-08-23 22:21:31

标签: javascript jquery

我正在尝试创建一个脚本,根据是否选择了某个选项来管理内容的可见性。我遇到的问题是我的if / else语句无法正常运行。

当选中最后一个.provider-info单选按钮时显示div .radial-container(即"我想保留我的号码")。

当从父容器中删除类slideUp时,它应该.select,但它不会。

我进行了一些实验,并且能够通过不同的代码获得我正在寻找的功能:

 $(function() {
  $('.radial-container').on('click', function() {
    $(this).addClass('select').siblings().removeClass('select');
    if($('.radial-container').last().hasClass('select')) {
      $(this).children('.provider-info').slideDown(300);
    } else {
      $('.provider-info').slideUp(300);
    }
  })
})

但上述细分市场的问题在于它仅适用于Unlimited Line 2.无限制的Line 1基本上失去了功能。

如何修复此代码以使if / else语句正常运行?我只希望在选择第二个单选按钮时可以看到div .provider-info

谢谢,

-M



$(function() {
  $('.radial-container').on('click', function() {
    $(this).addClass('select').siblings().removeClass('select');
    if($('.radial-container').hasClass('select')) {
      $(this).children('.provider-info').slideDown(300);
    } else {
      $('.provider-info').slideUp(300);
    }
  })
})

.phn-option-container {
	display:block;
}

.phn-unl {
	position:relative;
	margin:40px 0;
}

.phn-unl:after {
	content:'';
	display:block;
	position:relative;
	background:#e8e8e8;
	height:1px;
	top:30px;
	clear:both;
}

.radial-container {
	display:block;
	cursor: pointer;
	clear:both;
}

.phn-radio-container {
	margin:10px;
	clear:both;
}

.phn-unl > h4 {
	position:relative;
	left:10px;
	font-weight:600;
	color:#22ddc0;
}

.radial-container p {
	position:relative;
	float:left;
	left:25px;
	top:17px;
	color:#787878;
}

.radial-container.select .phn-radial .phn-center-dot {
	display:block;
}

.phn-radial {
	position:relative;
	float:left;
	height:35px;
	width:35px;
	padding:2px;
	margin:10px 0;
	border:5px solid #e8e8e8;
	border-radius:50%;
	left:10px;
	clear:both;
	cursor:pointer;
}

.phn-center-dot {
	display:none;
	position:relative;
	height:21px;
	width:21px;
	background-color:#E16E5B;
	border-radius:50%;
}

.provider-info label {
	color:#787878;
	margin:25px 0 0 60px;
}

.provider-info label span {
	position:relative;
	color:#E16E5B;
	top:-3px;
}

.provider-info input {
	background-color:transparent;
	border-width:0 0 2px;
	border-color:#787878;
	border-radius:0;
	margin-left:10px;
	width:270px;
	font-size:16px;
}

::-webkit-input-placeholder {
   font-style: italic;
}
:-moz-placeholder {
   font-style: italic;
}
::-moz-placeholder {
   font-style: italic;
}
:-ms-input-placeholder {
   font-style: italic;
}

.provider-info input:focus {
	border-color:#22ddc0;
	outline:0;
}

.provider-info {
	display:none;
	clear:both;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<div class="phn-option-container">
          <div class="phn-unl" data-unl-line="1">
            <h4>Unlimited Line 1</h4>
            <div class="radial-container select">
              <div class="phn-radial">
                <div class="phn-center-dot"></div>
              </div>
              <p>I would like a <strong>new</strong> number</p>
            </div>
            <div class="radial-container">
              <div class="phn-radial">
                <div class="phn-center-dot"></div>
              </div>
              <p>I would like to <strong>keep</strong> my number</p>
              <div class="provider-info">
                <div>
                  <label>Current Number: <span>*</span></label>
                  <input type="text" placeholder="e.g. (555) 555-5555"/>
                </div>
                <div>
                  <label>Current Provider: <span>*</span></label>
                  <input type="text" placeholder="e.g. Verizon"/>
                </div>
              </div>
            </div>
          </div>
          <div class="phn-unl" data-unl-line="2">
            <h4>Unlimited Line 2</h4>
            <div class="radial-container select">
              <div class="phn-radial">
                <div class="phn-center-dot"></div>
              </div>
              <p>I would like a <strong>new</strong> number</p>
            </div>
            <div class="radial-container">
              <div class="phn-radial">
                <div class="phn-center-dot"></div>
              </div>
              <p>I would like to <strong>keep</strong> my number</p>
              <div class="provider-info">
                <div>
                  <label>Current Number: <span>*</span></label>
                  <input type="text" placeholder="e.g. (555) 555-5555"/>
                </div>
                <div>
                  <label>Current Provider: <span>*</span></label>
                  <input type="text" placeholder="e.g. Verizon"/>
                </div>
              </div>
            </div>
          </div>
        </div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

你想用吗? $(this)。孩子们在else块中也是?

 $(this).children('.provider-info').slideDown(300);
} else {
  $(this).children('.provider-info').slideUp(300);

答案 1 :(得分:0)

这应该做的工作:

$('.radial-container').on('click', function() {
    $(this).addClass('select')
           .siblings('.radial-container')
           .removeClass('select')
           .find('.provider-info')
           .slideUp(300);
    $('.provider-info', this).slideDown(300);
});

修正演示:

&#13;
&#13;
$(function() {
    $('.radial-container').on('click', function() {
        $(this).addClass('select')
               .siblings('.radial-container')
               .removeClass('select')
               .find('.provider-info')
               .slideUp(300);
        $('.provider-info', this).slideDown(300);
    });
});
&#13;
.phn-option-container {
	display:block;
}

.phn-unl {
	position:relative;
	margin:40px 0;
}

.phn-unl:after {
	content:'';
	display:block;
	position:relative;
	background:#e8e8e8;
	height:1px;
	top:30px;
	clear:both;
}

.radial-container {
	display:block;
	cursor: pointer;
	clear:both;
}

.phn-radio-container {
	margin:10px;
	clear:both;
}

.phn-unl > h4 {
	position:relative;
	left:10px;
	font-weight:600;
	color:#22ddc0;
}

.radial-container p {
	position:relative;
	float:left;
	left:25px;
	top:17px;
	color:#787878;
}

.radial-container.select .phn-radial .phn-center-dot {
	display:block;
}

.phn-radial {
	position:relative;
	float:left;
	height:35px;
	width:35px;
	padding:2px;
	margin:10px 0;
	border:5px solid #e8e8e8;
	border-radius:50%;
	left:10px;
	clear:both;
	cursor:pointer;
}

.phn-center-dot {
	display:none;
	position:relative;
	height:21px;
	width:21px;
	background-color:#E16E5B;
	border-radius:50%;
}

.provider-info label {
	color:#787878;
	margin:25px 0 0 60px;
}

.provider-info label span {
	position:relative;
	color:#E16E5B;
	top:-3px;
}

.provider-info input {
	background-color:transparent;
	border-width:0 0 2px;
	border-color:#787878;
	border-radius:0;
	margin-left:10px;
	width:270px;
	font-size:16px;
}

::-webkit-input-placeholder {
   font-style: italic;
}
:-moz-placeholder {
   font-style: italic;
}
::-moz-placeholder {
   font-style: italic;
}
:-ms-input-placeholder {
   font-style: italic;
}

.provider-info input:focus {
	border-color:#22ddc0;
	outline:0;
}

.provider-info {
	display:none;
	clear:both;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<div class="phn-option-container">
          <div class="phn-unl" data-unl-line="1">
            <h4>Unlimited Line 1</h4>
            <div class="radial-container select">
              <div class="phn-radial">
                <div class="phn-center-dot"></div>
              </div>
              <p>I would like a <strong>new</strong> number</p>
            </div>
            <div class="radial-container">
              <div class="phn-radial">
                <div class="phn-center-dot"></div>
              </div>
              <p>I would like to <strong>keep</strong> my number</p>
              <div class="provider-info">
                <div>
                  <label>Current Number: <span>*</span></label>
                  <input type="text" placeholder="e.g. (555) 555-5555"/>
                </div>
                <div>
                  <label>Current Provider: <span>*</span></label>
                  <input type="text" placeholder="e.g. Verizon"/>
                </div>
              </div>
            </div>
          </div>
          <div class="phn-unl" data-unl-line="2">
            <h4>Unlimited Line 2</h4>
            <div class="radial-container select">
              <div class="phn-radial">
                <div class="phn-center-dot"></div>
              </div>
              <p>I would like a <strong>new</strong> number</p>
            </div>
            <div class="radial-container">
              <div class="phn-radial">
                <div class="phn-center-dot"></div>
              </div>
              <p>I would like to <strong>keep</strong> my number</p>
              <div class="provider-info">
                <div>
                  <label>Current Number: <span>*</span></label>
                  <input type="text" placeholder="e.g. (555) 555-5555"/>
                </div>
                <div>
                  <label>Current Provider: <span>*</span></label>
                  <input type="text" placeholder="e.g. Verizon"/>
                </div>
              </div>
            </div>
          </div>
        </div>
&#13;
&#13;
&#13;