刚开始学习如何制作一个crud功能。我的创建功能有一个收音机盒,有三个选项 - 个人,团队和公司。我想要做的是选择其中一个选项,让字段自动更改。例如。个人询问姓名,公司,电话,电子邮件和地址,而公司只询问公司,电话,电子邮件和地址。我已经完成了大部分语法,并且表单响应将数据放在mysql中。只是单选按钮不响应显示/隐藏某些表单。
<!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>
$('#contact_Type >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>
其他表格包括公司,职位,电话,电子邮件,地址和关闭。
<input type="submit" class="btn btn-primary col-md-2 col-md-offset-10" value="submit" />
</form>
</div>
</div>
</body>
</html>