jquery .submit();

早过忘川 提交于 2019-12-25 02:15:53

问题


Since Instamapper is closing down and I am doing this project where I am trying to get to the Moon counting how much km I have walked I tried to make my web app, which I could use instead. I got everything working except I wanted to automatically submit my form with data every 5 sec. I tried to use .submit() but it isn't working.

EDIT: The submit() function is not working, here is the error from the console:

Uncaught TypeError: Property 'submit' of object # is not a function

Here is the code:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"     type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    setTimeout(function(){
        $('form#target').submit();
    }, 5000);
    });
    </script>
<?php
if(isset($_POST['submit'])) {
    echo 'works';
}
$device = 'vPhone';
$date = new DateTime();
$timest = $date->getTimestamp();
?>
</head>

<body>
<form action="" method="post" id="target">
<input type="text" value="<?=$timest?>" name="timestamp" />
<input type="text" value="<?=$device?>" name="device" />
<input type="submit" name="submit" id="submit" />
</form>
<script>
var x=document.getElementById("target");
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition);
    } else {x.innerHTML="Geolocation is not supported by this browser.";}
  function showPosition(position)
    {
      x.innerHTML += "<input type='text' name='lat' value='" + position.coords.latitude + 
  "' disable><br> <input type='text' name='lat' value='" + position.coords.longitude + "'     disable> "; }
</script>
</body>
</html>

回答1:


It's name="submit" id="submit" that's causing the problem here. Change that to something else - anything, really, I used name="notsubmit" id="notsubmit" - and your form gets auto-submitted.

Source: http://api.jquery.com/submit/#comment-106178333

Apparantly these attributes somehow override the jQuery submit function so that it is no longer accessible.




回答2:


try adding an action to your form



来源:https://stackoverflow.com/questions/13750541/jquery-submit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!