[英]Javascript : Submitting a form outside the actual form doesn't work
I'm trying to achieve a fairly easy triggering mechanism for deleting multiple items from a tablegrid. 我正在尝试实现一种相当简单的触发机制,用于从tablegrid中删除多个项目。 If a user has enough access he/she is able to delete multiple users from a table. 如果用户具有足够的访问权限,则他/她可以从表中删除多个用户。 In the table I have set up checkboxes, one per row/user. 在表中,我设置了复选框,每行/用户一个。 The name of the checkboxes is UsersToDeletep[]
, and the value per row is the unique UserID
. 复选框的名称是UsersToDeletep[]
,每行的值是唯一的UserID
。
When a user clicks the button 'Delete selected users' a simple validation takes place to make sure at least one checkbox is selected. 当用户单击“删除选定的用户”按钮时,将进行简单的验证,以确保至少选中了一个复选框。 After that I call my simple function Submit(form)
. 之后,我调用我的简单函数Submit(form)
。 The function works perfectly when called within the form
-tags, where I also use it to delete a single user. 当在-tags form
调用该函数时,该函数运行完美,在这里我也使用它删除单个用户。
The function: 功能:
function Submit(form)
{
document.forms[form].submit();
}
I've also alerted document.forms[form]
. 我还提醒了document.forms[form]
。 The result is, as expected [object HTMLFormElement]
. 结果与预期的一样[object HTMLFormElement]
。 But for some reason the form just won't submit and a pagereload takes place. 但是由于某种原因,表单不会提交,因此发生了pagereload。 I'm a bit confused and can't seem to figure out what I'm doing wrong. 我有些困惑,似乎无法弄清楚我在做什么。
As requested: the calling-code: 根据要求:调用代码:
The code placed within onclick="javascript:"
: 放在onclick="javascript:"
的代码:
if(CheckMinimumCheckedCheckboxes('FormUserList', 1) == true)
{
if(confirm('Weet u zeker dat u de geselecteerde gebruiker(s) wilt verwijderen?'))
{
Submit('FormUserList');
}
}
else
{
alert('U dient minimaal 1 gebruiker te selecteren welke u wilt verwijderen.');
}
Separate functions in an external JS-file. 外部JS文件中的单独功能。
function CheckMinimumCheckedCheckboxes(formName, minAmount)
{
var totalChecked = 0;
var totalElements = document.forms[formName].length;
for(i = 0; i < totalElements; i++)
{
if(document.forms[formName][i].checked == true)
{
totalChecked++;
}
}
if(totalChecked >= minAmount)
{
return true;
}
return false;
}
function Submit(form)
{
document.getElementById(form).submit();
}
Try using the getElementById() Method: 尝试使用getElementById()方法:
http://www.w3schools.com/jsref/met_doc_getelementbyid.asp http://www.w3schools.com/jsref/met_doc_getelementbyid.asp
document.getElementById("frm1").submit();
http://www.w3schools.com/jsref/met_form_submit.asp http://www.w3schools.com/jsref/met_form_submit.asp
Never, ever, ever place an empty 'href=""' in your links and think you will fill it in later while later you decide to handle the link-element by an onclick-eventhandler... Seriously, it can save you hours of silly testing. 永远不要曾经在您的链接中放置一个空的'href =“”'并认为您稍后会填充它,而稍后您决定由onclick-eventhandler处理该链接元素...认真地说,它可以节省您的时间愚蠢的测试。 :/ :/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.