javascript插入表单提交

时间:2013-07-22 04:01:04

标签: javascript jquery html

尝试通过javascript通过表单发送数据,但它无法正常工作。任何想法为什么?

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<form method="get" action="test.php">
<input id="myvar" type="hidden" name="albumid" />

<button type="submit" id="btnsubmit">Submit</button>
</form>

<script type="text/javascript">
$("#btnsubmit").click(function(){     
    var album = '11';
    document.getElementById('myvar').value = album;

  });
</script>

test.php的

<?php echo $_GET["albumid"]; ?>

3 个答案:

答案 0 :(得分:1)

您需要停止提交事件的默认操作:

$("#btnsubmit").click(function(e){
    e.preventDefault();
    var album = '11';
    $('#myvar').val(album);
});

答案 1 :(得分:0)

提供id表格并提交表格

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<form id="myform" method="get" action="test.php">
<input id="myvar" type="hidden" name="albumid" />

<button type="submit" id="btnsubmit">Submit</button>
</form>

<script type="text/javascript">
$("#btnsubmit").click(function(){     
    var album = '11';
    document.getElementById('myvar').value = album;
    document.getElementById('myform').submit();

  });
</script>

答案 2 :(得分:0)

有几个问题:

  1. 加载jquery库时,http:中缺少src
  2. 由于您加载jquery使用它$('#myvar').val(album);。有关val()
  3. 的更多信息,请参阅

    这就是说试试

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    
    <form method="get" action="test.php">
        <input id="myvar" type="hidden" name="albumid" />
        <button type="submit" id="btnsubmit">Submit</button>
    </form>
    
    <script type="text/javascript">
        $('form').submit(function() {     
            var album = '11';
            $('#myvar').val(album);
        });
    </script>