我要实现的下面简单的JavaScript函数 submitForm()
根据 XMLHtt prequest
和 FORMDATA
。这个功能可以很好的第一<形式GT;
,但未能在第二个:该函数应使用 input.formaction 。
I want to implement the below simple JavaScript function submitForm()
based on XMLHttpRequest
and FormData
. This functions works well on the first <form>
but fails on the second one: the function should use input.formaction
instead of form.action
.
如何检测pssed的$ P $ &LT;输入&GT;
并获取相应的 formaction
?
How to detect the pressed <input>
and retrieve the corresponding formaction
?
(最让答案建议使用一个框架(如jQuery的),这样的处理,但我认为学习只是纯JavaScript比也在学习JS框架更容易。如果你确信这个片段可以用一个框架来写简单,请提出您的版本也请解释为什么你建议的框架是中肯/相关的/适合在这种情况下,我可能会决定去学习自己喜欢的JS框架... 编辑:我发现这个类似的问题: JQuery的GET formaction和formmethod )
( Most of SO answers advise to use a framework (as jquery) for such processing. But I think learning solely pure JavaScript is easier than learning also JS frameworks. If you are convinced that this snippet can be written simpler using a framework, please propose your version. Please explain also why your advised framework is pertinent/relevant/suitable in this case. I may decide to learn your favorite JS framework... I have found this similar question: JQuery get formaction and formmethod )
<!DOCTYPE html>
<html>
<head>
<script>
function submitForm(form)
{
var xhr = new XMLHttpRequest();
xhr.onload = function() { alert (xhr.responseText); }
xhr.open ("post", form.action, true);
xhr.send (new FormData (form));
return false;
}
</script>
</head>
<body>
<form action="first.php" onsubmit="submitForm(this);">
<fieldset>
<legend>First</legend>
<input type="text" name="data" />
<input type="submit" />
</fieldset>
</form>
<form onsubmit="submitForm(this);">
<fieldset>
<legend>Second</legend>
<input type="text" name="data" />
<input type="submit" value="A" formaction="second-A.php" />
<input type="submit" value="B" formaction="second-B.php" />
</fieldset>
</form>
</body>
</html>
(我已阅读 XMLHtt prequest到的POST HTML表单,的