如何使用jquery禁用或选择selectbox中的特定值?
我有一个输入字段:如何使用jquery禁用或选择selectbox中的特定值?,jquery,Jquery,我有一个输入字段: <input id="count" name="count" min="1" value="1" type="number" /> 这是小提琴:检查下面的代码并 我希望下面的代码可以帮助您 $(function() { $("#count").change(function() { var countValue = parseInt($("#count").val());
<input id="count" name="count" min="1" value="1" type="number" />
这是小提琴:检查下面的代码并
我希望下面的代码可以帮助您
$(function() {
$("#count").change(function() {
var countValue = parseInt($("#count").val());
$("#select option").each(function() {
var numberOption = parseInt($(this).attr("data-number"));
if (!isNaN(numberOption) && !isNaN(countValue) && countValue > numberOption)
$(this).hide();
else if (!isNaN(numberOption) && !isNaN(countValue))
{
$(this).show();
}
if (!isNaN(numberOption) && !isNaN(countValue) && countValue == numberOption)
$(this).attr("selected",true);
});
});
});
试一试:
$('input').change(function () {
var that = parseInt($(this).val(), 10);
$('#select option:gt(0)').each(function () {
$(this).prop('disabled', ($(this).data('number') < that)).prop('selected', ($(this).data('number') >= that && $(this).prev().data('number') < that));
})
if (that <= $('#select option:eq(1)').data('number')) $('#select option:eq(1)').prop('selected', true);
}).change()
你的条件有点矛盾。您会说“如果计数的输入值大于5,则禁用数据编号为5的选项”,但随后会说“对于输入值5,选择数据编号为5的选项”。因此,如果在输入中选择了5,是否应选择、禁用或同时禁用数据编号为5的选择?这就是我的想法,直到你能澄清我的问题:是的,如果输入值是5,那么选择选项5,但是如果输入值是6,那么禁用5。我看了你的小提琴,它几乎就是我想要的,非常感谢。这是另一条路。因此,如果我的输入为1,则选择4,不禁用任何内容。如果我的输入为2,则选择4,不禁用任何内容,依此类推。但是如果我的输入是5,那么5被选中,4是唯一一个被禁用的,像这样:?好的,再多做一个怎么样?如果你喜欢它,就把它作为答案。小提琴中没有任何东西被禁用。没有任何东西是禁用的。是否有任何功能被禁用(例如,如果输入中有5个)?如果它高于数据编号,则只有它将被禁用当我更改选项的顺序时,我试图使您的代码正常工作,但仍然找不到方法
$('input').change(function(){
if($('#count').val() > $('#select option:last-child()').data("number")) {
$('#select option[data-number="6"]').attr('disabled', 'disabled');
}
else {
$('#select option[data-number="6"]').removeAttr('disabled');
$('#select option[data-number="'+$('#count').val()+'"]').attr('selected', 'selected');
}
});
$(function() {
$("#count").change(function() {
var countValue = parseInt($("#count").val());
$("#select option").each(function() {
var numberOption = parseInt($(this).attr("data-number"));
if (!isNaN(numberOption) && !isNaN(countValue) && countValue > numberOption)
$(this).hide();
else if (!isNaN(numberOption) && !isNaN(countValue))
{
$(this).show();
}
if (!isNaN(numberOption) && !isNaN(countValue) && countValue == numberOption)
$(this).attr("selected",true);
});
});
});
$('input').change(function () {
var that = parseInt($(this).val(), 10);
$('#select option:gt(0)').each(function () {
$(this).prop('disabled', ($(this).data('number') < that)).prop('selected', ($(this).data('number') >= that && $(this).prev().data('number') < that));
})
if (that <= $('#select option:eq(1)').data('number')) $('#select option:eq(1)').prop('selected', true);
}).change()