表单在页面加载时而不是单击时运行提交脚本
Form running submission script on page load instead of on click
我正在尝试运行一个名为"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>
我需要这个提交脚本.js只有在按下按钮时才触发。相反,它会在页面加载后立即触发。我以为 .onclick = ...会解决这个问题,但唉。我在这里做错了什么?谢谢!
在库方面,我通过CDN使用Foundation框架和jQuery。
onclick 是一个 HTML 事件属性,即你会像这样使用的东西:
<button onclick="someFunction()">
你需要使用类似的东西
$('#submitButton').click(function() {
// do something
}
或
$('#submitButton').on("click", function() {
// do something
}
相关文章:
- 单击按钮前运行事件
- 单击链接时停止运行javascript
- 在单击事件中,第一次单击时激发代码一次,其余代码在所有单击时运行
- 在Node Webkit应用程序中从DOM单击按钮时运行批处理文件
- 计时器自动运行,而不是单击javascript按钮
- 运行“;非“;单击另一个函数中的函数仅一次
- 单击1个表单中的2个提交按钮中的1个时,如何运行脚本
- 在同一页面上单击按钮运行不同的代码
- 附加 jQuery 代码以在加载和按钮单击时运行
- 我需要在单击时运行两个函数.即使嵌套在一起,我也无法让它们都工作
- 如何制作按钮.单击“开始时不运行”
- 如何加载一个html页面并在其中只需单击一次即可运行函数
- 每次用户单击按钮时运行if语句检查器
- 运行Javascript更改RadioButtonList单击时的值
- 第二次单击时,使用JQUERY运行另一个函数
- 使用运行时创建的链接来触发ASP.Net中隐藏按钮的单击处理程序
- 单击javascript中的按钮运行python脚本
- 单击运行清除表单的JS函数的按钮后,视图无法恢复
- 单击运行两次
- jquery.单击“运行无事件函数”