gpt4 book ai didi

javascript - 如何根据从 AJAX 获得的 JSON 数据禁用选择选项?

转载 作者:太空狗 更新时间:2023-10-29 15:10:56 26 4
gpt4 key购买 nike

我有一个带有几个选项的下拉菜单。我想遍历它们并根据 Ajax 调用的结果在其中一些上支持 disabled 属性。

enter image description here

这就是我想要实现的目标。

enter image description here


示例可报告数据

s-00591 | true
s-00592 | false
s-00593 | false
s-00594 | false
s-00595 | false

HTML

<select class="rn-dropdown" id="rn-dd">
<option value="class-view">class view</option>
<!-- Students Populate Here -->
<option value="s-00591">Student S00591</option>
<option value="s-00592">Student S00592</option>
<option value="s-00593">Student S00593</option>
<option value="s-00594">Student S00594</option>
<option value="s-00595">Student S00595</option>
</select>

JS

success: function(reportable) {



$.each(reportable , function(i, v) {

var userId = v.userId;
var reportable = v.reportable;
var currentVal = userId;

console.log('start');
console.log(userId + " | " +reportable);
$('#rn-dd option[value="' + currentVal + '"]').prop('disabled', true);
console.log('end');

});



}

结果,

我在显示控制台时没有遇到任何错误。但是我一直看到我的下拉菜单没有像我希望的那样被禁用。

enter image description here

任何提示/帮助/建议对我来说意义重大。

最佳答案

  1. .prop('disabled', true); 我认为这是一个拼写错误或其他问题,因为它禁用了所有选项。

  2. 您的 jsonObj 中的 truefalse 值可能是 String。因此,无论值是 'true' 还是 'false',它都会被视为 true,所以 !reportable 始终为 false,这意味着它不会禁用任何选项。

您可能必须先检查它是否是一个字符串,例如:

reportable = (typeof v.reportable === 'string') ? v.reportable === 'true' : v.reportable

首先将其转换为 bool 值。

var reportable = [
{userId: 's-00591', reportable: 'true'},
{userId: 's-00592', reportable: 'false'},
{userId: 's-00593', reportable: 'false'},
{userId: 's-00594', reportable: 'false'},
{userId: 's-00595', reportable: 'false'}
];

// Check the diff between string and boolean.
var reportable2 = [
{userId: 's-00591', reportable: true},
{userId: 's-00592', reportable: false},
{userId: 's-00593', reportable: false},
{userId: 's-00594', reportable: false},
{userId: 's-00595', reportable: false}
];



$.each(reportable , function(i, v) {
var userId = v.userId;
var reportable = v.reportable;
var currentVal = userId;

console.log('start');
console.log(userId + " | " +reportable);
$('#rn-dd option[value="' + currentVal + '"]').prop('disabled', !reportable);
console.log('end');

});

$.each(reportable2 , function(i, v) {
var userId = v.userId;
var reportable = v.reportable;
var currentVal = userId;

console.log('start');
console.log(userId + " | " +reportable);
$('#rn-dd2 option[value="' + currentVal + '"]').prop('disabled', !reportable);
console.log('end');

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<select class="rn-dropdown" id="rn-dd">
<option value="class-view">class view</option>
<!-- Students Populate Here -->
<option value="s-00591">Student S00591</option>
<option value="s-00592">Student S00592</option>
<option value="s-00593">Student S00593</option>
<option value="s-00594">Student S00594</option>
<option value="s-00595">Student S00595</option>
</select>

<select class="rn-dropdown" id="rn-dd2">
<option value="class-view">class view</option>
<!-- Students Populate Here -->
<option value="s-00591">Student S00591</option>
<option value="s-00592">Student S00592</option>
<option value="s-00593">Student S00593</option>
<option value="s-00594">Student S00594</option>
<option value="s-00595">Student S00595</option>
</select>

关于javascript - 如何根据从 AJAX 获得的 JSON 数据禁用选择选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31654117/

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