在页面加载时表单运行提交脚本而不是单击

时间:2016-03-09 17:16:43

标签: javascript jquery forms parse-platform

我试图运行一个名为" submitScript"的脚本。当在此表单上按下提交按钮时。 submitScript与所有其他文件位于同一目录中。它是一个非常长的.js文件,它与Parse通信(我已经在文档的早期连接了Parse ID和秘密):

$('#submitButton').onclick = 
                        Parse.Cloud.run('importEvent', {
       eventName: 'Test Event',
       eventDate: 'May 4, 2016',
       posterTitles: 'AG1\nAG2\nAG3\nAG4',
       posterColleges: 'BIS\nENGR\nAG\nAG',
       judgeFirst: 'AG\nAG\nAG',
       judgeLast: 'AG1\nAG2\nAG3',
       judgeColleges: 'SCI\nENGR\nAG',
       judgeAssigned: '[1,4]\n[1]\n[2,3]'
       }).then(function(results) {
               // Event successfully created
               alert('success');
       }, function(error) {
               if (error.message == 'missing event name') {
                       alert('missing event name');
               }
               else if (error.message == 'eventname contains comma') {
                       alert('eventname contains comma');
               }
               else if (error.message == 'missing event date') {
                       alert('missing event date');
               }
               else if (error.message == 'missing poster titles') {
                       alert('missing poster titles');
               }
               else if (error.message == 'missing poster colleges') {
                       alert('missing poster colleges');
               }
               else if (error.message == 'missing judge firsts') {
                       alert('missing judge firsts');
               }
               else if (error.message == 'missing judge lasts') {
                       alert('missing judge lasts');
               }
               else if (error.message == 'missing judge colleges') {
                       alert('missing judge colleges');
               }
               else if (error.message == 'poster numbers are inconsistent') {
                       alert('poster numbers are inconsistent');
               }
               else if (error.message == 'judge numbers are inconsistent') {
                       alert('judge numbers are inconsistent');
               }
               else if (error.message == 'title contains comma') {
                       alert('title contains comma');
               }
               else if (error.message == 'pid invalid number of characters') {
                       alert('pid invalid number of characters');
               }
               else if (error.message == 'pid contains invalid character') {
                       alert('pid contains invalid character');
               }
               else if (error.message == 'first contains comma') {
                       alert('first contains comma');
               }
               else if (error.message == 'last contains comma') {
                       alert('last contains comma');
               }
               else if (error.message == 'jid invalid number of characters') {
                       alert('jid invalid number of characters');
               }
               else if (error.message == 'jid contains invalid character') {
                       alert('jid contains invalid character');
               }
               else if (error.message == 'invalid assignment format') {
                       alert('invalid assignment format');
               }
               else if (error.message == 'invalid assignment') {
                       alert('invalid assignment');
               }
               else {
                       // General error
                       alert('general error');
               }
});

这是与脚本相关联的HTML表单。它应该在按下提交按钮时运行。这个脚本实际上为我最终将使用表单捕获的字段发送硬编码值(我认为我在解决以下问题之前,先从表单字段中抓取数据然后将其发送出去)。

            <form>
                <input id="eventName" type="text" placeholder="Event Name" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Event Name'">
                <input id="eventDate" type="text" placeholder="Event Date" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Event Date'">
                <input id="posterTitles" type="textarea" placeholder="Poster Titles" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Poster Titles'">
                <input id="posterColleges" type="textarea" placeholder="Poster Colleges" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Poster Colleges'">
                <input id="judgeFirst" type="textarea" placeholder="Judge First" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Judge First'">
                <input id="judgeLast" type="textarea" placeholder="Judge Last" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Judge Last'">
                <input id="judgeColleges" type="textarea" placeholder="Judge Colleges" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Judge Colleges'">
                <input id="judgeAssigned" type="textarea" placeholder="Judge Assigned" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Judge Assigned'">
                <div id="submitButton">Submit</div>

                <script src = "submitScript.js">
                </script>

            </form>

我需要这个submitScript.js才能在按下按钮时触发。相反,它会在页面加载后立即触发。我以为.onclick = ...会解决这个问题,但是唉。我在这做错了什么?谢谢!

就库而言,我使用Foundation框架和jQuery通过CDN。

1 个答案:

答案 0 :(得分:0)

onclick是一个html事件属性,即你可以使用的东西:

<button onclick="someFunction()">

您需要使用类似

的内容
$('#submitButton').click(function() {
    // do something
}

OR

$('#submitButton').on("click", function() {
    // do something
}