我是javascript / jquery的新手,所以我可能会遗漏一些显而易见的东西,但我找到了解决方案,禁用了提交按钮,直到所有文本字段都填满为止,我找到了解决方案,禁用它直到文件是选择。但是,我的表单包含一个文件输入和3个文本字段,在找到所有文本字段和文件之前,我找不到禁用它的方法。
我正在使用的代码的提炼版本在这里:
HTML
<div>
<input type="file" /><br />
<input type="text" /><br />
<input type="text" /><br />
<input type="text" /><br />
<input type="submit" value="Upload" class="submit" id="submit" disabled="disabled" />
</div>
JS
$('.submit').click(function() {
var empty = $(this).parent().find("input").filter(function() {
return this.value === "";
});
if(empty.length) {
$('.submit').prop('disabled', false);
}
});
})()
感谢您的帮助
答案 0 :(得分:2)
尝试在这些字段上捕获事件并使用其他函数检查空值,请参阅下面的代码:
$(':input').on('change keyup', function () {
// call the function after
// both change and keyup event trigger
var k = checking();
// if value inc not 0
if (k) $('.submit').prop('disabled', true);
// if value inc is 0
else $('.submit').prop('disabled', false);
});
// this function check for empty values
function checking() {
var inc = 0;
// capture all input except submit button
$(':input:not(:submit)').each(function () {
if ($(this).val() == "") inc++;
});
return inc;
}
这只是一个例子,但逻辑有点像这样。
更新: 活动委派。您可能需要阅读 this
// document -> can be replaced with nearest parent/container
// which is already exist on the page,
// something that hold dynamic data(in your case form input)
$(document).on('change keyup',':input', function (){..});
答案 1 :(得分:1)
请看这个小提琴https://jsfiddle.net/xG2KS/482/
$('input').on('change',function(){
var empty = $('div').find("input").filter(function() {
return this.value === "";
});
if(empty.length>0) {
$('.submit').prop('disabled', true);
}
else{
$('.submit').prop('disabled', false);
}
});
[1]:
答案 2 :(得分:1)
诀窍是
以下是HTML的修改版本:
<form id="test" method="post" enctype="multipart/form-data" action="">
<input type="file" name="file"><br>
<input type="text" name="name"><br>
<input type="text" name="email"><br>
<button name="submit" type="submit">Upload</button>
</form>
和一些纯JavaScript:
window.onload=init;
function init() {
var form=document.getElementById('test');
form.onsubmit=testSubmit;
function testSubmit() {
if(!form['file'].value) return false;
if(!form['name'].value) return false;
if(!form['email'].value) return false;
}
}
请注意,我已删除了HTML中的所有XHTML痕迹。当然,这不是必需的,但HTML5确实允许更简单的上述版本,没有JavaScript。只需使用required
属性:
<form id="test" method="post" enctype="multipart/form-data" action="">
<input type="file" name="file" required><br>
<input type="text" name="name" required><br>
<input type="text" name="email" required><br>
<button name="submit" type="submit">Upload</button>
</form>
如果必填字段为空并且适用于所有现代(不是IE8)浏览器,则会阻止表单提交。
答案 3 :(得分:1)
在input
和file
输入元素上收听text
事件,计算未填充输入的数量,并根据提交按钮设置disabled
属性那个数字。看看下面的演示。
$(':text,:file').on('input', function() {
//find number of unfilled inputs
var n = $(':text,:file').filter(function() {
return this.value.trim().length == 0;
}).length;
//set disabled property of submit based on number
$('#submit').prop('disabled', n != 0);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
<input type="file" /><br />
<input type="text" /><br />
<input type="text" /><br />
<input type="text" /><br />
<input type="submit" value="Upload" class="submit" id="submit" disabled="disabled" />
</div>
&#13;
答案 4 :(得分:1)
对于我的方法,如果所有条件都是array
,我宁愿使用true
来存储。然后使用every
确保所有内容均为true
$(function(){
function validateSubmit()
{
var result = [];
$('input[type=file], input[type=text]').each(function(){
if ($(this).val() == "")
result.push(false);
else
result.push(true);
});
return result;
}
$('input[type=file], input[type=text]').bind('change keyup', function(){
var res = validateSubmit().every(function(elem){
return elem == true;
});
if (res)
$('input[type=submit]').attr('disabled', false);
else
$('input[type=submit]').attr('disabled', true);
});
});