我有一个带单选按钮和表单的更新功能。用户单击更新按钮以更新其联系人详细信息。我有一个JQuery函数来根据用户选择的单选按钮选项更改表单。我先显示照片,然后再说明。
只有当用户选择另一个单选按钮时,返回到公司按钮,JQuery函数才会显示正确的表单。如何使它能立即显示正确的表格?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<!DOCTYPE html>
<html>
<head>
<title>Edit contacts</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>
<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').show();
$('#companyDiv').hide();
$('#titleDiv').hide();
$('#phoneDiv').show();
$('#emailDiv').show();
$('#addressDiv').show();
break;
}
})
}
</script>
</head>
<body>
<div class="container">
<div class="row">
<form method="post" class="form-horizontal col-md-6 col-md-offset-3">
<h2>Edit Contact</h2>
<div class="form-group">
<label for="input" class="col-sm-6 control-label">Changes to contact type?
</label>
<div id="contactType" class="col-sm-10">
<input type="radio" name="Contact_type" <?=$r['Contact_type']=="Individual" ? "Checked" : ""?> value="Individual">Individual
<input type="radio" name="Contact_type" <?=$r['Contact_type']=="Team" ? "Checked" : ""?> value="Team">Team
<input type="radio" name="Contact_type" <?=$r['Contact_type']=="Company" ? "Checked" : ""?> 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" value="<?php echo $r['Name']; ?>" 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" value="<?php echo $r['Company'] ?>" 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" value="<?php echo $r['Title'] ?>" 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" value="
<?php echo $r['Phone'] ?>" 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" value="
<?php echo $r['Email'] ?>" 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" value="<?php echo $r['Address'] ?>" placeholder="Address" />
</div>
</div>
<input type="submit" class="btn btn-primary col-md-2 col-md-offset-10" value="Update" />
</form>
</div>
</div>
</div>
</body>
</html>
答案 0 :(得分:1)
试试这个
$( document ).ready(function() {
listenForInputChanges($('#contactType > input').val());
});
$('#contactType >input').change(function(){
listenForInputChanges($(this).val());
});
function listenForInputChanges(inp)
{
switch(inp) {
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').show();
$('#companyDiv').hide();
$('#titleDiv').hide();
$('#phoneDiv').show();
$('#emailDiv').show();
$('#addressDiv').show();
break;
}
}
修改的 也可以像这样替换单选按钮上的速记声明
<input type="radio" name="Contact_type" <?php
($r['Contact_type']=="Individual") ? " checked " : ""?>
value="Individual">Individual
<input type="radio" name="Contact_type" <?php ($r['Contact_type']=="Team") ?
" checked" : ""?> value="Team">Team
<input type="radio" name="Contact_type" <?php
($r['Contact_type']=="Company") ? " checked " : ""?> value="Company">Company