我有这样的选择和选项:
<select id="vari" name="variation" data-variation="US Shoe Size (Men's)">
<option id="var8">8</option>
<option id="var8.5">8.5</option>
<option id="var9">9</option>
<option id="var9.5">9.5</option>
<option id="var10">10</option>
<option id="var10.5">10.5</option>
<option id="var11">11</option>
<option id="var11.5">11.5</option>
<option id="var12">12</option>
<option id="var13">13</option>
<option id="var14">14</option>
</select>
例如(隐藏跨度)是这样的:
<span class="get">8</span>
<span class="get">8.5</span>
想要找到所有选项并与跨度值进行比较。 如果某些选项具有与类 get 相同的值(例如),则可以更改此值(我已经创建了跨度) 它将禁用该选项。 但问题是它禁用了所有。
$('#vari').find('option').each(function() {
var x = $(this).val();
var y = $('.get').text();
if(x = y){
this.disabled=true;
}
});
这里出了什么问题?
解决方案:::
$('#vari').find('option').each(function() {
x = $(this).val();
disable = true;
$('.get').each(function() {
if(x === $(this).text()){
disable = false;
}
});
if(disable){
$(this).prop('disabled', true);
}
});
最佳答案
此时你需要一个双 ==
,x 和 y 总是等于你在 if 语句中设置的 x = y
(请注意 .get
是一个类,您的 jQuery $('.get').text()
可以将一组文本转换为裸文本,如果有多个跨度,请记住类 .get
.)
$('#vari').find('option').each(function() {
var x = $(this).val();
var y = $('.get').text();
if(x == y){
this.disabled=true;
}
});
关于JQuery 将选项与跨度值进行比较,如果为真,则将选项禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44485478/