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

时间:2020-12-31 20:17:08

I'm trying to run a script called "submitScript" when the submit button is pressed on this form. submitScript is in the same directory as all the other files. It's a pretty long .js file that communicates with Parse (I've already hooked up the Parse ID and secret earlier in the document):

我正在尝试在此表单上按下提交按钮时运行名为“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');
               }
});

Here's the HTML form that the script is associated with. It should run when the submit button is pressed. This script actually sends hardcoded values for the fields I will eventually capture with the form (I thought I'd solve the question below before moving on to grabbing the data from the form fields and then sending it off).

这是脚本与之关联的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>

I need this submitScript.js to fire only if and when the button is pressed. Instead, it fires as soon as the page loads. I thought .onclick = ... would solve this, but alas. What am I doing wrong here? Thank you!

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

In terms of libraries I'm using the Foundation framework and jQuery via CDN.

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

1 个解决方案

#1


0  

onclick is an html event attribute, ie something you'd use like this:

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

<button onclick="someFunction()">

You need to use something like

你需要使用类似的东西

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

OR

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

#1


0  

onclick is an html event attribute, ie something you'd use like this:

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

<button onclick="someFunction()">

You need to use something like

你需要使用类似的东西

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

OR

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