英文:
Enable submit button if atleast one field has filled value / or a selected dropdown
问题
I have a <select>
tag along with <input>
, as below, but it doesn't work.
如下所示,我有一个包含<select>
和<input>
的标签,但它不起作用。
How can I enable the submit button if at least one field has a value or a selected option? How can I enable the submit button on multiple (and/&
in general) fields selection?
如果至少有一个字段具有值或已选择的选项,如何启用提交按钮?如何在选择多个(通常是and/&
)字段时启用提交按钮?
Any help is appreciated!
感谢任何帮助!
$(document).ready(function() {
$('.field input,.field select').keyup(function() {
var hasValue = $('#username,#password,#position').filter((index, input, select) => input.value.length > 0).length;
$('.actions input').attr('disabled', hasValue ? false : 'disabled');
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='form'>
<form>
<div class='field'>
<label for="username">Username</label>
<input id="username" type="text" />
</div>
<div class='field'>
<label for="password">Password</label>
<input id="password" type="password" />
</div>
<div class='field'>
<select id="position">
<option value="">position</option>
<option value="1">first</option>
<option value="2">second</option>
<option value="3">third</option>
</select>
</div>
<div class='actions'>
<input type="submit" value="Login" disabled="disabled" />
</div>
</form>
</div>
英文:
I have a <select>
tag along with <input>
, as below, but it doesn't work.
How can I enable the submit button if at least one field has a value or a selected option? How can I enable the submit button on multiple (and/&
in general) fields selection?
Any help is appreciated!
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
$('.field input,.field select').keyup(function() {
var hasValue = $('#username,#password,#position').filter((index, input, select) => input.value.length > 0).length;
$('.actions input').attr('disabled', hasValue ? false : 'disabled');
});
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='form'>
<form>
<div class='field'>
<label for="username">Username</label>
<input id="username" type="text" />
</div>
<div class='field'>
<label for="password">Password</label>
<input id="password" type="password" />
</div>
<div class='field'>
<select id="position">
<option value="">position</option>
<option value="1">first</option>
<option value="2">second</option>
<option value="3">third</option>
</select>
</div>
<div class='actions'>
<input type="submit" value="Login" disabled="disabled" />
</div>
</form>
</div>
<!-- end snippet -->
答案1
得分: 3
我会在form
上使用input
和change
事件,而不是在字段上使用keyup
事件,因为当然可以使用鼠标选择内容。我还会使用prop
而不是attr
来设置disabled
属性:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
$('form').on("input change", function() {
var hasValue = $('#username,#password,#position').filter((index, input, select) => input.value.length > 0).length;
$('.actions input').prop('disabled', !hasValue);
});
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='form'>
<form>
<div class='field'>
<label for="username">Username</label>
<input id="username" type="text" />
</div>
<div class='field'>
<label for="password">Password</label>
<input id="password" type="password" />
</div>
<div class='field'>
<select id="position">
<option value="">position</option>
<option value="1">first</option>
<option value="2">second</option>
<option value="3">third</option>
</select>
</div>
<div class='actions'>
<input type="submit" value="Login" disabled="disabled" />
</div>
</form>
</div>
<!-- end snippet -->
另外,我认为在选择要检查的字段时可能会使用更通用的选择器,并且在知道需要启用表单时停止检查:
<!-- begin snippet: js hide: true console: false babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
$('form').on("input change", function() {
var hasValue = false;
$(this).find("input[type=text], input[type=password], select").each(function() {
if (this.value) {
hasValue = true;
return false;
}
});
$(this).find('.actions input').prop('disabled', !hasValue);
});
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='form'>
<form>
<div class='field'>
<label for="username">Username</label>
<input id="username" type="text" />
</div>
<div class='field'>
<label for="password">Password</label>
<input id="password" type="password" />
</div>
<div class='field'>
<select id="position">
<option value="">position</option>
<option value="1">first</option>
<option value="2">second</option>
<option value="3">third</option>
</select>
</div>
<div class='actions'>
<input type="submit" value="Login" disabled="disabled" />
</div>
</form>
</div>
<!-- end snippet -->
英文:
I would use input
and change
on the form
, rather than keyup
on the fields, since of course you can pick things with the mouse. I'd also use prop
, not attr
, to set disabled
:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
$('form').on("input change", function() {
var hasValue = $('#username,#password,#position').filter((index, input, select) => input.value.length > 0).length;
$('.actions input').prop('disabled', !hasValue);
});
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='form'>
<form>
<div class='field'>
<label for="username">Username</label>
<input id="username" type="text" />
</div>
<div class='field'>
<label for="password">Password</label>
<input id="password" type="password" />
</div>
<div class='field'>
<select id="position">
<option value="">position</option>
<option value="1">first</option>
<option value="2">second</option>
<option value="3">third</option>
</select>
</div>
<div class='actions'>
<input type="submit" value="Login" disabled="disabled" />
</div>
</form>
</div>
<!-- end snippet -->
Separately, I think I'd probably use a more general selector when selecting the fields to check, and stop the check as soon as I know I need to enable the form:
<!-- begin snippet: js hide: true console: false babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
$('form').on("input change", function() {
var hasValue = false;
$(this).find("input[type=text], input[type=password], select").each(function() {
if (this.value) {
hasValue = true;
return false;
}
});
$(this).find('.actions input').prop('disabled', !hasValue);
});
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='form'>
<form>
<div class='field'>
<label for="username">Username</label>
<input id="username" type="text" />
</div>
<div class='field'>
<label for="password">Password</label>
<input id="password" type="password" />
</div>
<div class='field'>
<select id="position">
<option value="">position</option>
<option value="1">first</option>
<option value="2">second</option>
<option value="3">third</option>
</select>
</div>
<div class='actions'>
<input type="submit" value="Login" disabled="disabled" />
</div>
</form>
</div>
<!-- end snippet -->
答案2
得分: 2
您可以使用 change 事件而不是 keyup 事件。
$(document).ready(function() {
$('.field input,.field select').change(function() {
var hasValue = $('#username,#password,#position').filter((index, input, select) => input.value.length > 0).length;
$('.actions input').attr('disabled', hasValue ? false : 'disabled');
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='form'>
<form>
<div class='field'>
<label for="username">Username</label>
<input id="username" type="text" />
</div>
<div class='field'>
<label for="password">Password</label>
<input id="password" type="password" />
</div>
<div class='field'>
<select id="position">
<option value="">position</option>
<option value="1">first</option>
<option value="2">second</option>
<option value="3">third</option>
</select>
</div>
<div class='actions'>
<input type="submit" value="Login" disabled="disabled" />
</div>
</form>
</div>
英文:
You can use the change event instead of the keyup
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
$( '.field input,.field select' ).change(function() {
var hasValue = $('#username,#password,#position').filter((index, input, select) => input.value.length > 0).length;
$('.actions input').attr('disabled', hasValue ? false : 'disabled');
});
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='form'>
<form>
<div class='field'>
<label for="username">Username</label>
<input id="username" type="text" />
</div>
<div class='field'>
<label for="password">Password</label>
<input id="password" type="password" />
</div>
<div class='field'>
<select id="position">
<option value="">position</option>
<option value="1">first</option>
<option value="2">second</option>
<option value="3">third</option>
</select>
</div>
<div class='actions'>
<input type="submit" value="Login" disabled="disabled" />
</div>
</form>
</div>
<!-- end snippet -->
答案3
得分: 1
你只需使用 change
而不是 keyup
。
$(document).ready(function() {
$('.field input,.field select').change(function() {
var hasValue = $('#username,#password,#position').filter((index, input ,select) => input.value.length > 0).length;
$('.actions input').attr('disabled', hasValue ? false : 'disabled');
});
});
英文:
You just need to use change
rather than keyup
$(document).ready(function() {
$('.field input,.field select').change(function() {
var hasValue = $('#username,#password,#position').filter((index, input ,select) => input.value.length > 0).length;
$('.actions input').attr('disabled', hasValue ? false : 'disabled');
});
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论