编辑记录时,我希望下拉菜单显示以前选择的值。
代码如下:-
<div class="modal fade" id="edit_model" tabindex="-1" role="dialog" aria-hidden="true" style="display: none;">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h6 class="modal-title" >Model Title</h6>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<form action="filename.php" method="post">
<div class="form-group row">
<div class="col-md-1"></div>
<div class="col-md-3">
<input type="text" name="first_name" placeholder="First Name" id="first_name" class="form-control rs_input" required >
</div>
<div class="col-md-3">
<input type="text" name="last_name" placeholder="Last Name" id="last_name" class="form-control rs_input" required >
</div>
<select class="form-control" id="dropdown_id" name="dropdown_name">
<option >Select Value</option>
<?php
$dropdown = mysqli_query($connect, "SELECT * FROM table") or die(mysqli_error($connect));
while ($fetch_data = mysqli_fetch_array($country)) {
?>
<option value="<?php echo $fetch_data['id']; ?>"><?php echo $fetch_data['name']; ?></option>
<?php
} ?>
</select>
</div>
</div>
</div>
</div>
</div>
jQuery:-
$('.class_name').on('dblclick',function(){
var value = $(this).attr("id");
$.ajax({
url:"ajax/filename.php",
method:"POST",
data:{value:value},
dataType:"json",
success:function(data){
$('#first_name').val(data.first_name);
$('#last_name').val(data.last_name);
$('#dropdown_id').val(data.dropdown_value);
)};
)};
为澄清起见,我希望在用户编辑记录时在下拉列表中选择以前选择的值(记录中存在的值)。任何帮助表示赞赏。
答案 0 :(得分:6)
您需要指定selected
标签,否则它将仅填充下拉列表。
因此,在循环值时,是否需要检查必须选择哪个值(如果它是用户特定的)。
if (some condition)
{
<option selected="selected" ...
}
else
{
<option ...
}
答案 1 :(得分:4)
您需要检查以前选择的值。请参见下面的代码:
<?php
$dropdown = mysqli_query($connect, "SELECT * FROM table") or die(mysqli_error($connect));
while ($fetch_data = mysqli_fetch_array($country))
{
if(*previously selected*)
{
echo '<option selected value="<?php echo $fetch_data['id']; ?>"><?php echo $fetch_data['name']; ?></option>'
}
else
{
echo '<option value="<?php echo $fetch_data['id']; ?>"><?php echo $fetch_data['name']; ?></option>'
}
}
?>
用相关检查替换先前选择的,以查看该值是否为先前选择的值。如果您需要帮助,我将需要查看记录的内容,如果需要,请告诉我。