简体   繁体   English

Angular JS:基于单击单选按钮相对显示文本框并更改div的背景颜色

[英]Angular JS : show text box relatively based on click on radio button and change background color of div

image 图片

I am new to angularjs. 我是angularjs的新手。 as shown in the image , by default linkedin radio button is selected. 如图中所示,默认情况下已选择linkedin单选按钮。

but when i select on facebook radio button then two textboxes appears for 1 sec and hide the linked in textbox. 但是,当我在Facebook单选按钮上选择时,两个文本框将显示1秒钟,并隐藏文本框中的链接。

when i click on linkedin button then linked in textbox should appear and other textbox should get disappear and when i click on facebook radio button then facebook textbox should appear. 当我单击linkedin按钮时,应在文本框中出现链接,而其他文本框应消失,而当我单击facebook单选按钮时,应出现facebook文本框。

here is JSFiddle 这是JSFiddle

Thank You. 谢谢。

  .social_icon_radio_toggle { float: left; margin-right: 20px; } label { float: left; width: 35px; height: 35px; overflow: hidden; cursor: pointer !important; margin-right: 5px; } span { text-align: center; font-size: 15px; margin: auto; display: block; } input { position: absolute; } //wheather radio button is checked or not input:checked + span { background-color: $linked_in_bg_color; color: #fff; min-height: 100%; width: 35px; line-height: 33px; } input:not(:checked + span) { background-color: #edf1f2; color: #58666e; } .linkedin { background-color: #edf1f2; color: #58666e; border: thin #a8a8a8 solid; color: #a8a8a8; line-height: 33px; } .facebook { background-color: #edf1f2; color: #58666e; border: thin #ced9db solid; line-height: 35px; } 
 <div class="col-lg-10 col-md-10 col-sm-10 col-xs-10 set_padding_0 social_icons_section"> <div class="social_icon_radio_toggle"> <label class="linkedin"> <input type="radio" name="registration_options" checked="checked" ng-click="option='linkedin'" ng-init="option='linkedin'"> <span><i class="fa fa-linkedin"></i> </span></label> <label class="facebook"> <input type="radio" name="registration_options" ng-click="option='facebook'"> <span><i class="fa fa-facebook"></i> </span></label> </div> <div> <input class="form-control margin_top_4" placeholder="www.linkedin.com/example" type="text" ng-show="option == 'linkedin'" ng-hide="option == 'facebook'"> </div> <input class="form-control margin_top_4" placeholder="www.facebook.com/example" type="text" ng-show="option == 'facebook'" ng-hide="option == 'linkedin'"> </div> 

You should use the directive ng-model to track the value of your 'option' property. 您应该使用指令ng-model来跟踪“ option”属性的值。 Then set per every radio button the corresponding ng-value. 然后为每个单选按钮设置相应的ng值。

And take into account, that ng-show and ng-hide works in similar ways. 并考虑到ng-show和ng-hide的工作方式相似。 It is not necessary to use both at the same time. 不必同时使用两者。 ng-hide = hide the element if the expression = true ng-show = show the element if the expression = true ng-hide =如果表达式= true,则隐藏元素ng-show =如果表达式= true,则显示元素

<label class="linkedin" >
<input type="radio" name="registration_options"  ng-model="option" ng-value="'linkedin'">
<span><i class="fa fa-linkedin"></i> </span></label>
<label class="facebook" >
<input type="radio" name="registration_options" ng-value="'facebook'" ng-model="option">
 <span><i class="fa fa-facebook"></i> </span></label>

<input class="form-control margin_top_4" placeholder="www.linkedin.com/example" type="text" ng-show="option == 'linkedin'">
<input class="form-control margin_top_4" placeholder="www.facebook.com/example" type="text" ng-show="option == 'facebook'">

Working Sample: https://plnkr.co/edit/ezDAwU41vYKrw7DwWJAB 工作示例: https : //plnkr.co/edit/ezDAwU41vYKrw7DwWJAB

  .social_icon_radio_toggle{ float: left; margin-right: 20px; } label { float:left;width:35px; height:35px;overflow:hidden;cursor: pointer !important; margin-right: 5px;} span {text-align:center;font-size: 15px;margin:auto;display: block;} input {position:absolute;} //wheather radio button is checked or not input:checked + span {background-color:$linked_in_bg_color;color:#fff; min-height: 100%; width: 35px; line-height: 33px;} input:not(:checked + span) {background-color:#edf1f2;color:#58666e;} .linkedin {background-color:#edf1f2;color: #58666e; border: thin #a8a8a8 solid; color: #a8a8a8;line-height: 33px;} .facebook {background-color:#edf1f2;color: #58666e;border: thin #ced9db solid; line-height: 35px;} 
 <div class="col-lg-10 col-md-10 col-sm-10 col-xs-10 set_padding_0 social_icons_section"> <div class="social_icon_radio_toggle"> <label class="linkedin"> <input type="radio" name="registration_options" checked="checked" ng-click="option='linkedin'" ng-init="option='linkedin'"> <span><i class="fa fa-linkedin"></i> </span></label> <label class="facebook" > <input type="radio" name="registration_options" ng-click="option='facebook'"> <span><i class="fa fa-facebook"></i> </span></label> </div> <div type="text" ng-show="option === 'linkedin'" > <input class="form-control " placeholder="www.linkedin.com/example" > </div> <div ng-show="option === 'facebook'"> <input class="form-control " placeholder="www.facebook.com/example" type="text" > </div> </div> 

Here is updated JSFiddle 这是更新的JSFiddle

Hope this helps. 希望这可以帮助。

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

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