我正在使用 value 文件 uploader 。但我想启动一个复选框,如果用户没有选中它,则禁用上传按钮。
我可以让它以常规形式工作,但不能使用值,因为我认为,他使用 div 元素作为表单属性..所以我猜我已经搞砸了我的代码。
在 html 中我有:
<form class="well">
<label>Upload a Single File</label>
<label class="checkbox">
<input id="tos" onclick="if(this.checked){this.form.qq-upload-button.disabled=false}else{this.form.qq-upload-button.disabled=true};this.blur();" checked="" value="1" name="tos" type="checkbox"> I have read and agree to the <a href="/tos">TOS</a>
</label>
<div id="file-uploader-demo1">
<noscript>
<p>Please enable JavaScript to use file uploader.</p>
<!-- or put a simple form for upload here -->
</noscript>
</div>
</form>
在 fileuploader.js 中(我有值代码)
template: '<div class="qq-uploader">' +
'<div class="qq-upload-drop-area"><span>Drop files here to upload</span></div>' +
'<div class="qq-upload-button btn btn-primary left">Upload a file</div><span class="help-inline"> Lets Do it!</span>' +
'<ul class="qq-upload-list"></ul>' +
'</div>',
你能看出我搞了什么吗?
本质上,如果用户没有选中该复选框,我希望禁用上传按钮。
对于常规表单,此代码有效:
<form class="well">
<label>Upload a Single File</label>
<input type="file" class="span3" placeholder="Click Here">
<span class="help-inline">Associated help text!</span>
<label class="checkbox">
<input id="tos" onclick="if(this.checked){this.form.btn.disabled=false}else{this.form.btn.disabled=true};this.blur();" checked="" value="1" name="tos" type="checkbox"> I have read and agree to the <a href="/tos">TOS</a>
</label>
<input name="btn" type="submit" class="btn btn-primary left" value="UPLOAD">
</form>
最佳答案
删除内联 onclick
处理程序并添加以下 JavaScript 代码:
$('#tos').on('change', function() {
$('.qq-upload-button').prop('disabled', !this.checked);
});
这会将复选框的“禁用”状态与否定的“选中”状态同步。
您的原始代码不起作用的原因是 this.form.qq-upload-button
是无效的 JavaScript。您必须使用 this.form['qq-upload-button']
来代替。然而,使用我上面发布的代码更更干净。
关于jquery - 如果未选中复选框,则禁用上传按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11165254/