[英]enabled submit button after checked and terms downloaded
我創建了一個復選框功能,一旦選中其中一個復選框,便可以啟用表單提交。 單擊此處,我想知道是否可以向此功能添加更多內容,一旦選中該復選框,它會自動下載文件,可以這樣做嗎?
var checkboxes = $("input[type='checkbox']"),
submitButt = $("input[type='submit']");
checkboxes.click(function() {
submitButt.attr("disabled", !checkboxes.is(":checked"));
});
是的,你可以做到。 只需編寫以下代碼
checkboxes.click(function(e) {
e.preventDefault(); //stop the browser from following
window.location.href = 'uploads/file.doc';
});
如果需要根據復選框下載不同的文件,也可以檢查每個復選框的ID。
checkboxes.click(function(e) {
e.preventDefault(); //stop the browser from following
if (this.id == 'option-1')
window.location.href = 'uploads/file.doc';
if (this.id == 'option-2')
window.location.href = 'uploads/file1.doc';
and so on.....
});
您可以使用html5中引入的download
屬性來執行此操作:
$(':checkbox').change(function() { $('<a>', { "id":"downloadFile", text:'download file!', href:'https://cdn2.iconfinder.com/data/icons/flat-ui-icons-24-px/24/eye-24-256.png'}) .prop('download', 'download') // add a download property .appendTo(document.body); // append to document. $('#downloadFile')[0].click();// apply a click to download. $('#downloadFile').remove(); // then remove it from dom });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type='checkbox'/>
添加更改偵聽器
$('#option-1').change(function() {
if($(this).is(':checked')){
// do downloading options
confirm("Are you sure want to download?");
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.