这是我的第一个php页面html部分。我有一个包含div和submit按钮的表单。我希望当按钮点击时,打开一个新页面(second.php)并显示div(id=orderList)的内容
<form id="orderForm" action="second.php">
<div class="col-md-5" id="orderList">
<h3 align="center">Sipariş Listesi</h3>
</div>
<div>
<button type="submit" id="firstConfirmButton" class="btn btn-primary btn-lg">Onayla</button>
</div>
</form>
这是javascript的一部分;
$("#orderForm").submit(function() {
var content = $('#orderList').html();
console.log(content); //(no problem everything is ok)
$('#confirmForm').val(content);
var content2 = $('#confirmForm').html();
console.log(content2); //(it is null)
});
这是second.php,我想在这里显示div(id=confirmForm)中的内容
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="script.js"></script>
<META name=viewport content="width=1024, initial-scale=1">
<META http-equiv="Content-Type" content="text/html; charset=utf-8" />
<META HTTP-EQUIV="Content-language" CONTENT="tr">
<link href="shift.css" rel="stylesheet">
<link rel="stylesheet" href="bootstrap.css">
<link rel="stylesheet" href="product.css">
</head>
<body>
<div id="confirmForm"> </div>
</body>
</html>
它不起作用,我该换什么?
如果$('#confirmForm')
不是输入项,则无法使用$('#confirmForm').val()
访问它;
您的Form缺少method
属性,请在方法中添加POST
,默认情况下为其GET
<form id="orderForm" method="post" action="second.php">
在orderForm
内部的first.php
中添加一个<textarea>
,使用jquery保存#ordelist
的内容,在第二页中可以使用$_POST['confirmForm']
获取;
<textarea name="confirmForm" id="confirmForm" cols="30" rows="10"></textarea>
在第二页你做这个
<div id="confirmForm"> <?php echo $_POST['confirmForm'] ?> </div>
您正试图将内容放入#confirmForm中,但第一个文件中没有该内容。
这里不需要JavaScript。使用$_['post']抓取div的内容
试试它的工作原理:
first.php
<html>
<head>
</head>
<form id="orderForm" action="second.php">
<div class="col-md-5" id="orderList">
<textarea name="confirmForm" id="confirmForm" cols="30" rows="10">Sipari Listesi</textarea>
</div>
<div>
<button type="submit" id="firstConfirmButton" class="btn btn-primary btn-lg">Onayla</button>
</div>
</form>
</html>
second.php
<html>
<body>
<?php
echo $_REQUEST[confirmForm];
?>
</body>
</html>
除了Saqueib所说的,我认为您还需要对jQuery进行更改。我认为您正在寻找html()方法,而不是val(),因为它看起来像是在试图更改显示的html,对吧?