表单提交按钮onclick不会调用我的JS函数 [英] Form submit button onclick does not call my JS function
问题描述
我有一个带有提交按钮的html表单.我在头中包含的文件中有一个js函数.我试图绑定按钮的onclick事件以调用该函数.但是,我尝试的所有方法均无效.
I have an html form with a submit button. I have a js function in a file that I include in the head. I am trying to bind the onclick event of the button to call that function. However, all the methods I tried did not work.
<form class="form-horizontal" role="form" id="form_newCours">
<div class="form-group">
<label class="control-label col-sm-2" for="discippline">Matiere:</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="discipline" placeholder="Matiere">
</div>
</div>
<div class="form-group" align="left">
<label class="control-label col-sm-2" for="datetimepicker">Date:</label>
<div class="input-group col-sm-4">
<input type="text" class="form-control" id="datetimepicker"/>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label><input type="checkbox" id="creneau_regulier"> Ce creneau regulier</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default" id="btn-newCours">Ajouter</button>
</div>
</div>
</form>
JS函数位于send_json_req.js中:
The JS function is in send_json_req.js:
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
$('#form_newCours').on('click',function (e) {
console.log("Ici");
// YK: Bellow is my code
var jsonString = JSON.stringify($('#form_newCours').serializeObject());
console.log($('#form_newCours').serialize());
$.ajax({
type: "POST",
data : jsonString,
url: "/cours/",
contentType: "application/json"
});
});
我也尝试使用onclick从按钮直接调用该函数,但是它不起作用.我究竟做错了什么 ?
I also tried to call the function directly from the button with onclick but it did not work. What am I doing wrong ?
推荐答案
这些是您遇到的问题:
- 将DOM读取/操作代码包装在
$(document).ready(function(){...});
中-这样可确保元素可用于操作.请参阅指南. - 您将click事件绑定到了表单,而不是按钮.
- 您需要使用事件处理程序中的
event.preventDefault();
取消表单提交的默认行为.请参阅文档.
- Wrap your DOM reading/manipulating code in
$(document).ready(function() { ... });
- this ensures the elements are available for manipulation. See guide. - You were binding the click event to the form, not the button.
- You need to cancel the default behaviour of the form submit using
event.preventDefault();
inside the event handler. See docs.
此修改后的代码应该可以正常工作-请花些时间来了解注释中的更改:
This edited code should work - please take the time to understand the changes written in comments:
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
// All your code that reads/changes the HTML goes in here:
$(document).ready(function() {
// this was previously bound to the form, not the submit button:
$('#btn-newCours').on('click', function(e) {
// this will stop the form from submitting normally and let you handle it yourself
e.preventDefault();
console.log("Ici");
// YK: Bellow is my code
var jsonString = JSON.stringify($('#form_newCours').serializeObject());
console.log($('#form_newCours').serialize());
$.ajax({
type: "POST",
data: jsonString,
url: "/cours/",
contentType: "application/json"
});
});
});
这里有一个小提琴.它不会执行任何操作,但是当您单击按钮时,浏览器的控制台将显示禁止的请求,表明正在尝试发送数据.
There's a fiddle here. It won't do anything but your browser's console will show a forbidden request when you click the button, showing that an attempt to send the data is occurring.
这篇关于表单提交按钮onclick不会调用我的JS函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!