尝试通过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"]; ?>
答案 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)
有几个问题:
http:
中缺少src
$('#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>