<!--js-->
function changeValue(dropdown,source) {
var option = dropdown.options[dropdown.selectedIndex].value;
if (option == '1' && source==0) {
var total = 20;
$("span").text(total);
$('.balance').attr({"min":0, "max":total}).on('input', function() {
var value = parseInt(this.value);
var otherInputs = $('.balance').not(this);
var remainderDiv;
var remainder, sum;
if (isNaN(value)) {
value = 0;
} else if (value > total) {
value = total;
} else if (value < 0) {
value = 0;
}
this.value = value;
remainder = total - value;
remainderDiv = remainder/otherInputs.length;
sum = value;
$.each(otherInputs, function(input) {
sum += Number(otherInputs[input].value);
});
if (sum > total) {
otherInputs.val(remainderDiv);
}
});
}else if (option == '2' && source==0) {
var total = 20;
$("span").text(total);
$('.balance').attr({"min":0, "max":total}).on('input', function() {
var value = parseInt(this.value);
var otherInputs = $('.balance').not(this);
var remainderDiv;
var remainder, sum;
if (isNaN(value)) {
value = 0;
} else if (value > total) {
value = total;
} else if (value < 0) {
value = 0;
}
this.value = value;
remainder = total - value;
remainderDiv = remainder/otherInputs.length;
sum = value;
$.each(otherInputs, function(input) {
sum += Number(otherInputs[input].value);
});
if (sum > total) {
otherInputs.val(remainderDiv);
}
});
}
}
<!--html-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select onchange="changeValue(this,0)">
<option value="1">1-person</option>
<option value="2">2-10 group</option>
</select>
<input type="number" class="balance">
<input type="number" class="balance">
<!--html-->
这就是我希望它留在选项和if语句的方式。我想这样的原因是因为这些字段是动态添加的。所以,如果你可以添加一个函数到现有的代码,让它改变值,当我改变选择选项,这将是伟大的。该代码的最上面是工作完美,它只是需要什么我问。获取此代码更新选择选项更改
真的不理解你实际需要什么。请详细说明 –
@Umair Farooq好的,如果你复制我已经运行的代码,你会看到当你改变选择选项时会发生什么。因此,如果您选择1人,然后选择2-10组,则为1人设置的值将保留在字段中,并且不会动态更改。 – Lee
要以什么价值动态地改变它? –