0

我正在尝试通过表单通过 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>

测试.php

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

3 回答 3

1

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

$("#btnsubmit").click(function(e){
    e.preventDefault();
    var album = '11';
    $('#myvar').val(album);
});
于 2013-07-22T04:20:25.960 回答
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>
于 2013-07-22T04:03:41.950 回答
0

有几个问题:

  1. 加载 jquery 库时您丢失http:src
  2. 由于您加载 jquery 使用它$('#myvar').val(album);。查看更多val()

话虽这么说尝试

<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>
于 2013-07-22T04:16:54.837 回答