首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JQuery函数对单选按钮没有响应

JQuery函数对单选按钮没有响应
EN

Stack Overflow用户
提问于 2018-02-15 07:30:14
回答 2查看 54关注 0票数 0

这对我来说是一个非常棘手的问题(或者说我是个彻头彻尾的哑巴)。尝试使用JQuery函数使其在用户选择某个单选按钮时,下面的表单会相应地更改(例如,个人可以看到姓名、公司、头衔、电话、电子邮件和地址。公司只能看到公司、电话、电子邮件和地址。但目前单击单选按钮没有任何效果。

我还想问: 1)是不是我的函数$('#contactType>input')。放在正确的地方(在中),还是应该放在其他地方? 2)我可以使用index.js文件来简化它吗?如果是这样,如何链接index.js?是不是只是

干杯。

代码语言:javascript
复制
$(document).ready(function() {
  listenForInputChanges();
})

function listenForInputChanges() {
  $('#contactType >input').change(function() {
    console.log('val is ' + $(this).val())
    switch ($(this).val()) {
      case 'individual':
        $('#nameDiv').show();
        $('#companyDiv').show();
        $('#titleDiv').show();
        $('#phoneDiv').show();
        $('#emailDiv').show();
        $('#addressDiv').show();
        break;

      case 'team':
        $('#nameDiv').show();
        $('#companyDiv').show();
        $('#titleDiv').hide();
        $('#phoneDiv').show();
        $('#emailDiv').show();
        $('#addressDiv').show();
        break;

      case 'company':
        $('#nameDiv').hide();
        $('#companyDiv').show();
        $('#titleDiv').hide();
        $('#phoneDiv').show();
        $('#emailDiv').show();
        $('#addressDiv').show();
        break;
    }
  })
}
代码语言:javascript
复制
<!DOCTYPE html>
<html>

<head>
  <title>Create new contact</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
  </script>


</head>

<body>
  <div class="container">
    <div class="row">
      <form method="post" class="form-horizontal col-md-6 col-md-offset-3">
        <h2>Motoko Insurance Contacts</h2>


        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
        </script>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />

        <div class="form-group">
          <label for="input" class="col-sm-6 control-label">What type of contact are you adding?</label>
          <div id="contactType" class="col-sm-10">
            <input type="radio" name="Contact_type" value="individual"> Individual
            <input type="radio" name="Contact_type" value="team"> Team
            <input type="radio" name="Contact_type" value="company"> Company</div>
        </div>


        <div id="nameDiv" class="form-group">
          <label for="input1" class="col-sm-2 control-label">Name</label>
          <div class="col-sm-10">
            <input type="text" name="name" class="form-control" id="input1" placeholder="Name" />
          </div>
        </div>

        <div id="companyDiv" class="form-group">
          <label for="input1" class="col-sm-2 control-label">Company</label>
          <div class="col-sm-10">
            <input type="text" name="comp" class="form-control" id="input1" placeholder="Company" />
          </div>
        </div>

        <div id="titleDiv" class="form-group">
          <label for="input1" class="col-sm-2 control-label">Title</label>
          <div class="col-sm-10">
            <input type="text" name="title" class="form-control" id="input1" placeholder="Title" />
          </div>
        </div>

        <div id="phoneDiv" class="form-group">
          <label for="input1" class="col-sm-2 control-label">Phone</label>
          <div class="col-sm-10">
            <input type="int" name="urstel" class="form-control" id="input1" placeholder="Phone" />
          </div>
        </div>

        <div id="emailDiv" class="form-group">
          <label for="input1" class="col-sm-2 control-label">E-Mail</label>
          <div class="col-sm-10">
            <input type="email" name="email" class="form-control" id="input1" placeholder="E-mail" />
          </div>
        </div>

        <div id="addressDiv" class="form-group">
          <label for="input1" class="col-sm-2 control-label">Address</label>
          <div class="col-sm-10">
            <input type="text" name="location" class="form-control" id="input1" placeholder="Address" />
          </div>
        </div>


        <input type="submit" class="btn btn-primary col-md-2 col-md-offset-10" value="submit" />
      </form>

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

</html>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-02-15 07:33:31

你错过了jQuery库。记住将其添加到页面的<head>部分:

代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

代码语言:javascript
复制
$(document).ready(function() {
  listenForInputChanges();
})

function listenForInputChanges() {
  $('#contactType >input').change(function() {
    console.log('val is ' + $(this).val())
    switch ($(this).val()) {
      case 'individual':
        $('#nameDiv').show();
        $('#companyDiv').show();
        $('#titleDiv').show();
        $('#phoneDiv').show();
        $('#emailDiv').show();
        $('#addressDiv').show();
        break;

      case 'team':
        $('#nameDiv').show();
        $('#companyDiv').show();
        $('#titleDiv').hide();
        $('#phoneDiv').show();
        $('#emailDiv').show();
        $('#addressDiv').show();
        break;

      case 'company':
        $('#nameDiv').hide();
        $('#companyDiv').show();
        $('#titleDiv').hide();
        $('#phoneDiv').show();
        $('#emailDiv').show();
        $('#addressDiv').show();
        break;
    }
  })
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>

<head>
  <title>Create new contact</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
  </script>


</head>

<body>
  <div class="container">
    <div class="row">
      <form method="post" class="form-horizontal col-md-6 col-md-offset-3">
        <h2>Motoko Insurance Contacts</h2>


        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
        </script>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />

        <div class="form-group">
          <label for="input" class="col-sm-6 control-label">What type of contact are you adding?</label>
          <div id="contactType" class="col-sm-10">
            <input type="radio" name="Contact_type" value="individual"> Individual
            <input type="radio" name="Contact_type" value="team"> Team
            <input type="radio" name="Contact_type" value="company"> Company</div>
        </div>


        <div id="nameDiv" class="form-group">
          <label for="input1" class="col-sm-2 control-label">Name</label>
          <div class="col-sm-10">
            <input type="text" name="name" class="form-control" id="input1" placeholder="Name" />
          </div>
        </div>

        <div id="companyDiv" class="form-group">
          <label for="input1" class="col-sm-2 control-label">Company</label>
          <div class="col-sm-10">
            <input type="text" name="comp" class="form-control" id="input1" placeholder="Company" />
          </div>
        </div>

        <div id="titleDiv" class="form-group">
          <label for="input1" class="col-sm-2 control-label">Title</label>
          <div class="col-sm-10">
            <input type="text" name="title" class="form-control" id="input1" placeholder="Title" />
          </div>
        </div>

        <div id="phoneDiv" class="form-group">
          <label for="input1" class="col-sm-2 control-label">Phone</label>
          <div class="col-sm-10">
            <input type="int" name="urstel" class="form-control" id="input1" placeholder="Phone" />
          </div>
        </div>

        <div id="emailDiv" class="form-group">
          <label for="input1" class="col-sm-2 control-label">E-Mail</label>
          <div class="col-sm-10">
            <input type="email" name="email" class="form-control" id="input1" placeholder="E-mail" />
          </div>
        </div>

        <div id="addressDiv" class="form-group">
          <label for="input1" class="col-sm-2 control-label">Address</label>
          <div class="col-sm-10">
            <input type="text" name="location" class="form-control" id="input1" placeholder="Address" />
          </div>
        </div>


        <input type="submit" class="btn btn-primary col-md-2 col-md-offset-10" value="submit" />
      </form>

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

</html>

票数 5
EN

Stack Overflow用户

发布于 2018-02-15 07:40:16

要将JavaScript放在一个单独的文件中,只需在与.html文件相同的目录中创建一个javascript文件(例如:'index.js')。在此之后,将您创建的Javascript放入该文件中。现在,您可以使用脚本标记将其包括在内:

代码语言:javascript
复制
<script type="text/javascript" src="index.js"></script>

src指向相对于.html文件的index.js文件的路径。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48797918

复制
相关文章

相似问题

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