如何通过更改输入值来禁用启用按钮。我想通过远程或从其他输入字段更改其值来在#second
只读输入字段中启用“更改”按钮。
$(document).ready(function(){
$('#button').attr('disabled',true);
});
$(document).on('change keyup blur','#first',function(){
var second= $('#second').val();
if($(this).val() != '' ){
sum = parseFloat($(this).val()) + parseFloat(second);
$('#second').val(sum);
}
});
$("#second").on('change',function(){
var second= $(this).val();
var third= $('#third').val();
if (second == third) {
$('#button').attr('disabled',false);
} else {
$('#button').attr('disabled',true);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" id="first" placeholder="first">
<input type="number" id="second" value="5" readonly>
<input type="number" id="third" value="10" readonly>
<input type="button" id="button" value="button" />
答案 0 :(得分:1)
试试这个
https://www.w3schools.com/code/tryit.asp?filename=G5YTJB50T072
在您的代码中使用了keyup,change,blur,这使计算不正确,并且始终在document.ready中使用jquery代码(例如change函数)。
谢谢
<script>
$(document).ready(function(){
$('#button').attr('disabled',true);
$("#second").on('change',function(){
var second = $(this).val();
var third = $('#third').val();
if (second == third) {
$('#button').attr('disabled',false);
} else {
$('#button').attr('disabled',true);
}
});
});
$(document).on('keyup','#first',function(){
var second= $('#second').val();
if($(this).val() != ''){
sum = parseFloat($(this).val()) + parseFloat(second);
$('#second').val(sum);
$('#second').val(sum).trigger('change');
}
});
</script>
答案 1 :(得分:0)
您的问题在这一行:
$('#second').val(sum);
更改输入值不会触发更改事件。因此,您可以自己触发它:
$('#second').val(sum).trigger('change');
$('#button').attr('disabled', true);
$(document).on('change keyup blur', '#first', function () {
var second = $('#second').val();
if ($(this).val() != '') {
sum = parseFloat($(this).val()) + parseFloat(second);
$('#second').val(sum).trigger('change');
}
});
$("#second").on('change', function () {
var first = $("#first").val();
var second = $(this).val();
var third = $('#third').val();
if (second == third) {
$('#button').attr('disabled', false);
} else {
$('#button').attr('disabled', true);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" id="first" placeholder="first">
<input type="number" id="second" value="5" readonly>
<input type="number" id="third" value="10" readonly>
<input type="button" id="button" value="button">