【问题标题】:Change forms at the start rather than after change在开始而不是在更改之后更改表格
【发布时间】:2018-07-26 21:18:36
【问题描述】:

我有一个带有单选按钮和表单的更新功能。用户单击更新按钮来更新他们的联系方式。我有一个 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>

【问题讨论】:

  • 你是什么意思?
  • 我的错。我的意思是当更新页面打开时。我希望显示正确的表格,而不是需要再次单击单选按钮的上面照片的情况,然后才会显示正确的表格。
  • 但是当onload它会自动选择正确的收音机?

标签: php jquery forms echo


【解决方案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

【讨论】:

  • 谢谢杰罗姆!不幸的是,上述方法不起作用。 :(
  • @kerrysun 我已经做了一些更改可能会再试一次。
猜你喜欢
  • 2013-11-01
  • 2021-09-19
  • 1970-01-01
  • 2012-06-12
  • 1970-01-01
  • 1970-01-01
  • 2014-10-20
  • 1970-01-01
  • 2021-11-20
相关资源
最近更新 更多