gpt4 book ai didi

javascript - 填充选择选项错误 Javascript

转载 作者:行者123 更新时间:2023-11-28 12:59:29 25 4
gpt4 key购买 nike

我正在开发一个学校管理系统,在解决了所有错误之后,我似乎无法修复这个错误!在我的系统中,您可以从下拉列表中选择 child 的 parent (父亲和母亲),信息正确保存到数据库中,但在编辑/查看时出现问题。

关闭第一个 child 的模式并移至第二个 child ,然后返回第一个 child 后,父亲和母亲将显示前一个 child 的 parent 数据。

我尝试在ajax填充下拉列表之前清除选择/取消选择下拉列表的值,但无济于事。这是代码,它仅在您选择子行上的编辑按钮时执行:

$.ajax({
url: base_url + 'student/fetchStudentData/'+studentId,
type: 'post',
cache: false,
dataType: 'json',
success:function(response){

// UNSELECT THE DROP DOWNS TO RESET
//$('#editSex').find("option:selected").prop('selected',false);
//$('#editFatherId').find("option:selected").prop('selected',false);
//$('#editMotherId').find("option:selected").prop('selected',false);

$("#editFname").val(response.fname);
$("#editLname").val(response.lname);
$("#editDob").val(response.dob);

// NOW SELECT THE OPTION
$('#editSex option[value='+ response.sex +']').attr('selected','selected');
$('#editFatherId option[value='+ response.father_id +']').attr('selected','selected');
$('#editMotherId option[value='+ response.mother_id +']').attr('selected','selected');

$("#editAge").val(response.age);
$("#editContact").val(response.contact);
$("#editEmail").val(response.email);
$("#editAddress").val(response.address);
$("#editCity").val(response.city);
$("#editCountry").val(response.country);
$("#editRegisterDate").val(response.register_date);
$("#editClassName").val(response.class_id);

$("#editSectionName").load('student/fetchClassSection/'+response.class_id, function(i) {
$("#editSectionName").val(response.section_id);
});

$("#student_photo").attr('src', base_url + response.image);

$("#editClassName").unbind('change').bind('change', function() {
var class_id = $(this).val();
$("#editSectionName").load('student/fetchClassSection/'+class_id);
});

HTML 和 PHP:

        <!-- FATHER -->
<div class="form-group">
<label for="editFatherId" class="col-sm-4 control-label">Father</label>
<div class="col-sm-8">
<select class="form-control" name="editFatherId" id="editFatherId">
<option value="">Select</option>
<?php foreach ($parentsDataMale as $key => $value) { ?>
<option value="<?php echo $value['parents_id'] ?>"><?php echo $value['fname'] . ' ' . $value['lname'] ?></option>
<?php } // /forwach ?>
</select>
</div>
</div>

<!-- MOTHER -->
<div class="form-group">
<label for="editMotherId" class="col-sm-4 control-label">Mother</label>
<div class="col-sm-8">
<select class="form-control" name="editMotherId" id="editMotherId">
<option value="">Select</option>
<?php foreach ($parentsDataFemale as $key => $value) { ?>
<option value="<?php echo $value['parents_id'] ?>"><?php echo $value['fname'] . ' ' . $value['lname'] ?></option>
<?php } // /forwach ?>
</select>
</div>
</div>

问题图片:第一个 child : Child 1 Edit

第二个 child : Child 2 Data

回到第一: Back to Child 1

最佳答案

您应该在使用 json 数据“选择”一个选项之前“取消选择”所有选项。否则,您可能会面临选择多个人的风险……这很奇怪。

此外,使用 bool 属性而不是使用属性。

// NOW SELECT THE OPTION
$('#editSex option').prop('selected',false);
$('#editSex option[value='+ response.sex +']').prop('selected',true);
$('#editFatherId option').prop('selected',false);
$('#editFatherId option[value='+ response.father_id +']').prop('selected',true);
$('#editMotherId option').prop('selected',false);
$('#editMotherId option[value='+ response.mother_id +']').prop('selected',true);

关于javascript - 填充选择选项错误 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52181277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com