これが私のJavaScriptです:
<script>
//Make sure DOM is ready before mucking around.
$(document).ready(function()
{
console.log('DOM is ready!');
var socket = io.connect('http://localhost:8080');
//Each document field will have the following identifiers:
//classCode, description, professor, file
function uploadForm()
{
console.log('Creating FileReader object...');
var reader = new FileReader();
reader.onload = function(evt)
{
console.log('Read complete.');
console.log('Creating JSON object...')
var documentData =
{
'classCode':$('#classCode').val(),
'professor':$('#professor').val(),
'description':$('#description').val(),
'file':
{
'data': evt.target.result,
'metadata': document.getElementById('file').files[0]
},
'dateUploaded':new Date(),
'rating':0
};
console.log(documentData);
console.log('Adding document.');
socket.emit('addDocument', documentData);
};
console.log('Reading as binary string...');
reader.readAsDataURL(document.getElementById('file').files[0]);
};
});
</script>
私のHTMLフォーム:
<form onsubmit = 'uploadForm()'>
<input type = 'text' placeholder = 'Class code' id = 'classCode' required/>
<input type = 'text' placeholder = 'Document description' id = 'description' required/>
<input type = 'text' placeholder = 'Professor' id = 'professor' required/>
<input type = 'file' id = 'file' required/>
<input type = 'submit' value = 'Upload!'/>
</form>
このコードは、以前は要素のイベントをuploadForm()
介して呼び出していたときに機能していましたが、要素で必要な属性チェックを無視していました。だから今、私はの送信イベントを呼び出そうとしていますが、正しく実行されていません。イベントは終了後に呼び出されるはずですが、そうではありません。私はすでにjQueryを試しましたが無駄になりました。.click
<input type='submit'>
<form>
<input>
uploadForm()
<form>
reader.onload
reader.readDataAsURL()
.submit()
編集:ついに私の電話でエラー記録をキャッチしました。Uncaught ReferenceError: uploadForm is not defined