jQuery表单提交通过单击链接无法正常工作

时间:2019-01-18 17:56:46

标签: jquery html

我有一个隐藏输入类型的表单。我想通过单击表单外部的链接来提交它。

<form id="form" method="post" action="phpfile.php">
    <input type="hidden" name="ids" value="somevalue" />
</form>

<a id="link" href="">link</a>

<script>
$(document).ready(function () {
    $('#link').click(function(){
        $('#form').submit();
    });
});
</script>

但是当我通过“提交”按钮提交表单时,效果很好。

2 个答案:

答案 0 :(得分:0)

Here is your code using another element

<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
</head>
<body>
<form id="form" method="post" action="phpfile.php">
    <input type="hidden" name="ids" value="somevalue" /><input type="submit" value="Go">
</form>

<div id="link" href="">link</div>

<script>
$(document).ready(function () {
    $('#link').on('click',function(){
    alert('a');
        $('#form').submit();
    });
});
</script>
</body>
</html>

答案 1 :(得分:0)

Here's the code using anchor as triggering element.

<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
</head>
<body>
<form id="form" method="post" action="phpfile.php">
    <input type="hidden" name="ids" value="somevalue" /><input type="submit" value="Go">
</form>

<a id="link" href="">link</a>

<script>
$(document).ready(function () {
    $('#link').on('click',function(e){
       e.preventDefault();
        $('#form').submit();
    });
});
</script>
</body>
</html>