[英]handle javascript function inside loop
I am using javascript function inside of while loop. 我在while循环内使用javascript函数。 That loop prints list of records and edit button. 该循环将打印记录列表和“编辑”按钮。 When click on edit button there is function(Let say editBtnClicked(id)). 当单击编辑按钮时,具有功能(假设editBtnClicked(id))。 Problem: It is getting called to there but page is refreshing automatically. 问题:在那里被调用,但是页面自动刷新。
What is happening over there I couldn't find? 我找不到那里发生了什么? Someone have a look on this. 有人看看这个。
function editBtnClicked(id) {
console.log("Edit Section");
$(".showData").css("display", "none");
$(".showInputField").css("display", "block"); }
<button id="edit" name="Edit" value="Edit" class="edit btn btn-info"onclick="editBtnClicked('<%=id%>');">
By defualt, button
elements are type="submit"
. 通过默认, button
元素是type="submit"
。 If you don't want it to be a submit button, use type="button"
instead: 如果您不希望它成为提交按钮,请改用type="button"
:
<button type="button" id="edit" name="Edit" value="Edit" class="edit btn btn-info"onclick="editBtnClicked('<%=id%>');">
If for some reason you want it to continue to be a submit button, but you want to prevent submission: In your onclick
attribute, pass the event
into your function: 如果出于某种原因您希望它继续成为一个提交按钮,但又希望阻止提交:在onclick
属性中,将该event
传递到您的函数中:
<button id="edit" name="Edit" value="Edit" class="edit btn btn-info"onclick="editBtnClicked(event, '<%=id%>');">
That event
will exist within the context created for that call, either as a global (Chrome, IE) or as a local (Firefox). 该event
将以全局(Chrome,IE)或本地(Firefox)的形式存在于为该调用创建的上下文中。
Then in your handler, use preventDefault
: 然后在您的处理程序中,使用preventDefault
:
function editBtnClicked(event, id) {
$.Event(event).preventDefault(); // Note the jQuery event wrapper
console.log("Edit Section");
$(".showData").css("display", "none");
$(".showInputField").css("display", "block");
}
That said, I'd probably change this up completely and use event delegation instead of an onclick
attribute. 就是说,我可能会完全改变它,并使用事件委托而不是onclick
属性。
On some container all these buttons will be in: 在某些容器上,所有这些按钮都位于:
$("selector for the container").on("click", "button.edit", editBtnClicked);
...and when outputting them (note I've removed the id
— you said this was a loop, you can't have the same id
more than once!): ...并且在输出它们时(请注意,我已经删除了id
您说这是一个循环,您不能多次拥有相同的id
!):
<button type="button" type="button" value="Edit" class="edit btn btn-info" data-id="'<%=id%>'">
And in the function: 并在函数中:
function editBtnClicked() {
var id = $(this).attr("data-id"); // Get the ID
console.log("Edit Section");
$(".showData").css("display", "none");
$(".showInputField").css("display", "block");
}
1- attach preventDefault() to click event or return false at the end of function to prevent submitting. 1-将preventDefault()附加到click事件上,或在函数末尾返回false以防止提交。 In second case you should write onclick="return editBtnClicked('<%=id%>')" 在第二种情况下,您应该编写onclick =“ return editBtnClicked('<%= id%>')”
2- Do not put your function inside the loop. 2-不要将函数放入循环中。 Consider using ID inside your function so you wont need to repeat function inside the loop. 考虑在函数内使用ID,这样就无需在循环内重复函数。
Please use PreventDefault for reference : http://api.jquery.com/event.preventdefault/ 请使用PreventDefault作为参考: http ://api.jquery.com/event.preventdefault/
function editBtnClicked(e) {
e.preventDefault();
console.log("Edit Section");
$(".showData").css("display", "none");
$(".showInputField").css("display", "block");
}
<button id="edit" name="Edit" value="Edit" class="edit btn btn-info"onclick="editBtnClicked('<%=id%>');">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.