创建具有更改字段的函数 (Javascript?)

Create function with changing fields (Javascript?)

正在尝试实现联系人列表的创建功能。

我希望它的运作方式是第一个问题要求 'contact type' 包含三个字段 - 个人、团队和公司。

通过选择其中一个字段,下面的字段将会发生变化。个人会显示所有字段(姓名、公司、职位、phone、电子邮件、地址),而公司会要求不显示 name/title,而团队不会要求任何职位。所有这些字段 + 第一个问题已经是 mysql 中的行。

我不知道从哪里开始,它的语法应该如何工作以及如何将 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"/>   
<script> 
$( 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;
  }
})
}
</script>   

<div class="form-group">
 <label for="input" class="col-sm-2 control-label">What type of contact are you adding?</label>
 <div 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 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 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 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 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 class="form-group">
            <label for="input1" class="col-sm-2 control-label">Email</label>
            <div class="col-sm-10">
              <input type="email" name="email"  class="form-control" id="input1" placeholder="E Mail" />
            </div>
        </div>

        <div 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>    

使用 jQuery 这真的很容易 - 我创建了一个模拟示例,只是在您单击输入时隐藏和显示一些值。

$( document ).ready(function() {
  listenForInputChanges();
})

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

    case 'team':
      $('#nameDiv').show();
      $('#companyDiv').show();
    break;

    case 'company':
      $('#nameDiv').hide();
      $('#companyDiv').show();
    break;
    }
  })
} // end inputChanger
<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" checked> 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">
<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>

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