gpt4 book ai didi

javascript - 如果输入和复选框不为空,则启用提交按钮

转载 作者:行者123 更新时间:2023-11-29 23:57:00 26 4
gpt4 key购买 nike

下面我试图确保在允许用户提交之前我的复选框和输入都已填满。它忽略了我对复选框的检查,并在仅填写字段后打开提交。我做错了什么?

$('#first, #last, #pass').bind('keyup', function() {
if (allFilled()) $('#register').removeAttr('disabled');
});

function allFilled() {
var filled = true;

console.log($('#checkbox').prop('checked'))

$('body input').each(function() {
if ($(this).val() == '' && $('#checkbox').prop('checked') == false) filled = false;
});

return filled
};

html:

<body>
<form>
Username
<br />
<input type="text" id="first" name="first" />
<br /> First
<br />
<input type="text" id="last" name="last" />
<br /> Last
<br />
<input type="text" id="pass" name="pass" />
<br /> Password
<br />
<input type="checkbox" class="checkbox" id="checkbox" name="checkbox" />
<br />
<input type="submit" id="register" disabled value="Register" />
</form>
<div id="test">
</div>
</body>

最佳答案

As of jQuery 3.0, .bind() has been deprecated. It was superseded by the .on() method for attaching event handlers to a document since jQuery 1.7, so its use was already discouraged.

请不要使用已弃用的方法 bind() 使用 on() 而不是:

$('#first, #last, #pass').on('keyup', function() {
if (allFilled()) $('#register').removeAttr('disabled');
});

希望这对您有所帮助。

$('#first, #last, #pass').on('keyup', function() {
if (allFilled()) $('#register').removeAttr('disabled');
});

function allFilled() {
var filled = true;

$('body input').each(function() {
if ($(this).val() == '' && $('#checkbox').prop('checked') == false) filled = false;
});
return filled
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
Username
<br />
<input type="text" id="first" name="first" />
<br /> First
<br />
<input type="text" id="last" name="last" />
<br /> Last
<br />
<input type="text" id="pass" name="pass" />
<br /> Password
<br />
<input type="checkbox" class="checkbox" id="checkbox" name="checkbox" />
<br />
<input type="submit" id="register" disabled value="Register" />
</form>
<div id="test">
</div>

关于javascript - 如果输入和复选框不为空,则启用提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41350220/

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