html表单:具有预定义值的js submit()

时间:2016-01-29 20:09:35

标签: javascript html

我想使用没有提交按钮的html表单。 我使用submit()但我无法设置默认值:

<form id="form" action="index.php" method="post">
        <select name="date" size="1" onclick="javascript: submitform()" value="201601291010">
<option value="201601291010">2016-01-29 10:10 UTC</option>
<option value="201601291000">2016-01-29 10:00 UTC</option>
<option value="201601290950">2016-01-29 09:50 UTC</option>
        </select>

</form>     

<script type="text/javascript">
function submitform() 
{
  document.getElementById('form').submit();
}
</script>

<?php $date = $_POST['date']; echo ("date is $date"); ?>

$ date在加载时为空。如何在加载时将$ date设置为“201601291010”并由onclick重新定义?

1 个答案:

答案 0 :(得分:0)

如果您确实想要使用默认值提交,请使用onload事件。

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

<script>
$(document).ready(function(){
     submitform(); // this fires as soon as the page loads
     // if you want to wait until the user selects an option then
     // remove the above and uncomment the below.

     //$("#date").change(function(){
     //    submitform();
     //});
});

function submitform(){
    alert($("#date").val()); //alert to let you see the default value, remove as needed
    document.getElementById('form').submit();
}

</script>
</head>
<body>
<form id="form" action="index.php" method="post">
    <select id="date" name="date" size="1">
        <option selected="selected" value="201601291010">2016-01-29 10:10 UTC</option>
        <option value="201601291000">2016-01-29 10:00 UTC</option>
        <option value="201601290950">2016-01-29 09:50 UTC</option>
   </select>

 </form>     
</body>
</html>