gpt4 book ai didi

jQuery - 禁用/启用选择选项

转载 作者:行者123 更新时间:2023-12-03 22:08:40 28 4
gpt4 key购买 nike

我需要一些关于 jquery if 条件的帮助。我已经搜索和测试了几个小时,任何帮助都会很棒!我得到这个 HTML 代码:

<label id="label1" for="date_from">Value:</label>
<input value="" />

<select id="select1" name="select1">
<option>No Match</option>
<option value="1">Test</option>
<option value="2">Test 2</option>
<option value="3">Test 3</option>
</select>

<select id="select2" name="select2">
<option>No Match</option>
<option value="1">Test</option>
<option value="2">Test 2</option>
<option value="3">Test 3</option>
</select>

还有这个 jquery 函数:

if ( $('#label1').val() < 3 ) {
$('select').change(function() {

$('#select1, #select2').not(this)
.children('option[value=' + this.value + ']')
attr('disabled', true)
.siblings().removeAttr('disabled');

});
}
else {
$('select').change(function() {

$('#select1, #select2').not(this)
.children('option[value=' + this.value + ']')
.attr('disabled', false)
.siblings().removeAttr('disabled');

});
}

我是jquery的初学者,我不知道这段代码是否正确编写,因为它不起作用。

问题是:

当输入值小于3时,select2中的选择选项与select1中的相同,并且select2中的其余选项被禁用。当输入值大于 3 时,select1 和 select2 中的选项将启用。

最诚挚的问候,感谢您阅读这篇 jquery 新手帖子...

最佳答案

试试这个:

$(function(){
$("input").change(function(){
if ( $(this).val() < 3 ) {
$('#select2').prop('disabled', true);
$('#select1').on("change",function() {
$('#select2').val($(this).val());
});
}else {
$("#select1").off();
$('#select1, #select2').prop('disabled', false);
}
});
});

演示:http://jsfiddle.net/qhPp7/2/

关于jQuery - 禁用/启用选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11629669/

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