简体   繁体   English

Angular Radio按钮单击显示和隐藏不起作用

[英]Angular Radio button click show and hide not working

I'm beginner for the Angular and try to create Angular 5 ,when i click the radio button show and hide div area. 我是Angular的初学者,当我单击单选按钮显示和隐藏div区域时,尝试创建Angular 5。 ,I created customer-1 and customer-2 radio button when i click the customer-2 radio button is it not working (shown 1st div),That one is normal html to working fine, but it's not work for angular 5 , anyone know how to put that one correctly in Angular5? ,当我单击customer-2 单选按钮时,我创建了customer-1customer-2 单选按钮 (它显示为1st div),那是正常的html可以正常工作,但是对于angular 5却不起作用,有人知道如何正确地把那个放在Angular5中?

Thanks 谢谢

html HTML

<div class="modal fade cust-my-modal cust-my-modal-2" id="signup_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
        <h4 class="modal-title" id="myModalLabel">SIGN UP</h4>
      </div>
      <div class="modal-body">
        <div class="row">
          <div class="col-sm-12">
            <div class="top-radio">
              <div class="radio radio-primary">
                <input type="radio" name="radio1" id="radio1" onClick="changediv('nc_signup')" checked="checked" value="option1">
                <label for="radio1">
                 customers-1
                </label>
              </div>
              <div class="radio radio-primary">
                <input type="radio" name="radio1" id="radio2" onClick="changediv('cc_signup')" value="option2">
                <label for="radio2">
                 customers-2
                </label>
              </div>
            </div>
            <form class="fotm" id="cc_signup" style="display: none;" action="rfq.html">
              <div class="frm-btm-mrg">
                <div class="form-group">
                  <input class="form-control" placeholder="Email id" type="text">
                </div>
                <div class="form-group">
                  <input class="form-control" placeholder="Password"  type="password">
                </div>
                <div class="form-group">
                  <input class="form-control" placeholder="Re-enter password"  type="password">
                </div>

                <div class="form-group">
                  <input class="form-control" placeholder="Company name" type="text">
                </div>
                <div class="form-group">
                  <input class="form-control" placeholder="Website" type="text">
                </div>
                <div class="form-group">
                  <input class="form-control contact-num" placeholder="Contact number" type="text">
                </div>
                <div class="checkbox">
                  <input id="checkbox2" type="checkbox">
                  <label for="checkbox2">
                    I agree terms & conditions
                  </label>
                </div>
              </div>

              <button type="submit" class="btn">Sign up</button>
            </form>


            <form class="fotm" id="nc_signup" action="rfq.html">

              <div class="frm-btm-mrg-2">
                <div class="form-group">
                  <input class="form-control" placeholder="User name" type="text">
                </div>
                <div class="form-group">
                  <input class="form-control" placeholder="Email id" type="text">
                </div>
                <div class="form-group">
                  <input class="form-control" placeholder="Password"  type="password">
                </div>
                <div class="form-group">
                  <input class="form-control" placeholder="Re-enter password"  type="password">
                </div>
                <div class="checkbox">
                  <input id="checkbox3" type="checkbox">
                  <label for="checkbox3">
                    I agree terms & conditions
                  </label>
                </div>
              </div>

              <button type="submit" class="btn">Sign ups</button>
            </form>

          </div>
        </div>
      </div>
    </div>
  </div>
</div>

Live code- stackblitz 实时代码-Stackblitz

javascript JavaScript的

<script>  
    function changediv(divid){
        $("#nc_signup").hide();
        $("#cc_signup").hide();
        $("#"+ divid).show();

    }
</script>

  function changediv(divid) { $("#nc_signup").hide(); $("#cc_signup").hide(); $("#"+ divid).show(); } 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <div class="modal fade cust-my-modal cust-my-modal-2" id="signup_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="myModalLabel">SIGN UP</h4> </div> <div class="modal-body"> <div class="row"> <div class="col-sm-12"> <div class="top-radio"> <div class="radio radio-primary"> <input type="radio" name="radio1" id="radio1" onClick="changediv('nc_signup')" checked="checked" value="option1"> <label for="radio1"> customers-1 </label> </div> <div class="radio radio-primary"> <input type="radio" name="radio1" id="radio2" onClick="changediv('cc_signup')" value="option2"> <label for="radio2"> customers-2 </label> </div> </div> <form class="fotm" id="cc_signup" style="display: none;" action="rfq.html"> <div class="frm-btm-mrg"> <div class="form-group"> <input class="form-control" placeholder="Email id" type="text"> </div> <div class="form-group"> <input class="form-control" placeholder="Password" type="password"> </div> <div class="form-group"> <input class="form-control" placeholder="Re-enter password" type="password"> </div> <div class="form-group"> <input class="form-control" placeholder="Company name" type="text"> </div> <div class="form-group"> <input class="form-control" placeholder="Website" type="text"> </div> <div class="form-group"> <input class="form-control contact-num" placeholder="Contact number" type="text"> </div> <div class="checkbox"> <input id="checkbox2" type="checkbox"> <label for="checkbox2"> I agree terms & conditions </label> </div> </div> <button type="submit" class="btn">Sign up</button> </form> <form class="fotm" id="nc_signup" action="rfq.html"> <div class="frm-btm-mrg-2"> <div class="form-group"> <input class="form-control" placeholder="User name" type="text"> </div> <div class="form-group"> <input class="form-control" placeholder="Email id" type="text"> </div> <div class="form-group"> <input class="form-control" placeholder="Password" type="password"> </div> <div class="form-group"> <input class="form-control" placeholder="Re-enter password" type="password"> </div> <div class="checkbox"> <input id="checkbox3" type="checkbox"> <label for="checkbox3"> I agree terms & conditions </label> </div> </div> <button type="submit" class="btn">Sign ups</button> </form> </div> </div> </div> </div> </div> </div> 

The Angular way to do this is to put code in app.component.ts instead of in a script-tag with jquery selectors. 做到这一点的Angular方法是将代码放在app.component.ts中,而不是在带有jquery选择器的脚本标签中。

You should have two member variables in your class that are changed by a method in the component.ts-fil, and bind to that method by using (click) in the HTML. 您的类中应该有两个成员变量,它们由component.ts-fil中的方法更改,并通过使用HTML中的(单击)绑定到该方法。 To bind to variables in the code, use the [attr]-syntax. 要绑定到代码中的变量,请使用[attr]语法。

You can take a look at my fork of your code here https://stackblitz.com/edit/angular-ktt3ri 您可以在这里查看我的代码分支https://stackblitz.com/edit/angular-ktt3ri

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

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