如何将 ID 提交到表单名称中以提交特定表单?
function submitComment(id)
{
alert('comment on song id: '+[id]+'');
document.postcomment.submit();
}
我希望能够提交.. postcomment43 或 postcomment 42.. 无论 ID 的值与 postcomment 联合
尝试过这个:
function submitComment(id)
{
alert('comment on song id: '+[id]+'');
var formToSubmit = 'postcomment' + id;
alert( ''+ formToSubmit +'' );
document.formToSubmit.submit();
}
正确创建 formToSubmit 名称但不触发。如何正确地将 formToSubmit 变量放置在 document.form.submit 中
似乎只要输入 formToSubmit 就会查找名称为 formToSubmit 的表单
最佳答案
为您的表单提供唯一的 ID:
<form id="postcomment42" action="..."></form>
<form id="postcomment43" action="..."></form>
<form id="postcomment44" action="..."></form>
然后使用getElementById
函数以获得所需的形式:
function submitComment(id)
{
var formToSubmit = document.getElementById('postcomment' + id);
if (formToSubmit != null) formToSubmit.submit();
}
虽然我怀疑你的设计有问题。为什么有多种形式?难道您不能使用一个表单来提交评论,例如:
<form id="postcomment" action="comment?id=42" method="post"></form>
您能否提供更多有关界面外观以及您想要实现的目标的详细信息?
关于javascript 在 document.nameofformtosubmit.submit() 中使用变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1147265/