gpt4 book ai didi

javascript 提交()命令不发送我的帖子数据

转载 作者:行者123 更新时间:2023-11-30 11:30:16 29 4
gpt4 key购买 nike

来自 this earlier thread我以为我知道可以使用 javascript submit() 命令通过 POST 方法发送表单数据。但我无法让它工作。这个演示在目的方面没有明显的意义,但请耐心等待。我只是想测试这个似乎对我不起作用的特定命令。你能帮我么?单击常规提交按钮可以正常发送发布数据,但不能通过链接激活 javascript。

<html><body>

<?php
$self = $_SERVER['PHP_SELF'];
$posttext = file_get_contents('php://input');
echo "Received input: ".chr(34).$posttext.chr(34)."<br><br>";
echo "<form id='test' action='{$self}' method='post'>";
?>
Please fill in the fields with any text:<br><br>
foo: <input type="text" name="foofield"><br><br>
bar: <input type="text" name="barfield"><br><br>
<input type="submit" value="Submit button works"><br><br>
<a href="" id="submitlink">Submitting by JS not working – why?</a>
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
jQuery(function($) {
$(document).ready(function(){
$("a#submitlink").click(function(){
alert("Yes, the link has been clicked. It is not that.");
$("form#test").submit();
});
});
});
</script>
</body></html>

最佳答案

您需要:event.preventDefault(); <a> 中的默认行为.问题是如果你不这样做,页面会重新加载,这就是 <a> 的原因。标签确实如此。因此,即使您提交了数据,您也会刷新已提交数据丢失的页面。

$("a#submitlink").click(function(event){
event.preventDefault();
$("form#test").submit();
});

另一个解决方案是添加 #href<a>标签如:

<a href="#" id="submitlink">Submitting by JS not working – why?</a>

这也将阻止 <a>标签进行刷新。

关于javascript 提交()命令不发送我的帖子数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46501115/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com