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

时间:2018-09-03 08:13:42

标签: javascript html angular

我是Angular的初学者,当我单击单选按钮显示和隐藏div区域时,尝试创建Angular 5。 ,我点击{customer-2“单选按钮时,我创建了customer-1customer-2 单选按钮(显示为1st div),那是正常的HTML即可正常工作,但不适用于角度5, 有谁知道如何正确地将它放在Angular5中?

谢谢

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>

实时代码-stackblitz

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>

1 个答案:

答案 0 :(得分:1)

做到这一点的Angular方法是将代码放在app.component.ts中,而不是在带有jquery选择器的脚本标签中。

您的类中应该有两个成员变量,它们由component.ts-fil中的方法更改,并通过使用HTML中的(单击)绑定到该方法。要绑定到代码中的变量,请使用[attr]语法。

您可以在https://stackblitz.com/edit/angular-ktt3ri这里查看我的代码分支