Javascript 单击表单中的 div 元素后 this.form.submit() 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8382184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
this.form.submit() not working after clicking div element in form
提问by user701510
I was trying to test form submission using mouse clicks but the form doesn't seem to submit with vanilla javascript.
我试图使用鼠标点击测试表单提交,但表单似乎没有使用 vanilla javascript 提交。
I'm using this simple markup and code:
我正在使用这个简单的标记和代码:
<form name="form" id="price" action="" method="post">
<div class="category" name="price" value="50 dollars"
onClick="this.form.submit();"
>price</div>
</form>
<?php
echo $_POST['price'];
?>
I can submit the form with Jquery, but I don't understand why this.form.submit()
is not working with vanilla javascript? I'm using Chrome to test this.
我可以使用 Jquery 提交表单,但我不明白为什么this.form.submit()
不能使用 vanilla javascript?我正在使用 Chrome 来测试这个。
回答by dxh
A div
is not a form element. There is no this.form
for it.
Adiv
不是表单元素。没有this.form
它。
You can still do document.forms.form.submit()
(.form
since you have name="form"
)
你仍然可以做document.forms.form.submit()
(.form
因为你有name="form"
)
回答by buruzaemon
Your code might work if you tried something like this:
如果您尝试过这样的操作,您的代码可能会起作用:
onClick="document.forms["price"].submit();"
this
in your case actually refers to the div
tag, not the document object which contains the reference to the form itself.
this
在您的情况下,实际上是指div
标签,而不是包含对表单本身的引用的文档对象。
回答by user2629584
Use following code if your form name is "filter-form"
如果您的表单名称是“filter-form”,请使用以下代码
onclick="return document.forms.filter-form.submit();"
回答by Monarch Wadia
This is an older question, but this should work
这是一个较旧的问题,但这应该有效
onclick="this.parentNode.form.submit()"
回答by ThiefMaster
A div
isn't a form element (thus no .form
property) nor has it a value. I think you wanted <input>
instead of <div>
.
Adiv
不是表单元素(因此没有.form
属性)也没有值。我认为你想要<input>
而不是<div>
.