【发布时间】:2017-02-10 21:15:23
【问题描述】:
我有一个包含公司名称地址邮政编码等行的表格。
我有一个选项,如果您不知道完整的详细信息,您可以输入名称并单击搜索。这将通过 AJAX 获得具有相似名称的所有公司的列表并将它们返回到模式窗口(不确定这是否是最好的方法)
列表中的每个公司旁边都有一个按钮,用户可以选择该公司。
单击他们希望关闭表单的公司后,我希望完成原始页面上的地址详细信息。
问题在于,当我使用 method="post" 命令时,模式上的按钮被点击后正在刷新原始页面,这将删除任何其他已经处于原始形式的数据。
有没有办法关闭模态并更新原始表单而不刷新?
谢谢
这是我的原始页面:
<input type="text" id="text1"><button id="button"> Search </button>
<input type="text" id="Address1">
<input type="text" id="Address2">
<input type="text" id="Country">
<input type="text" id="PostCode">
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Companies Found</h4>
</div>
<div class="modal-body">
<div id="result"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
<script>
$('#button').click(function() {
var val1 = $('#text1').val();
$.ajax({
type: 'POST',
url: 'api.php',
data: { text1: val1},
success: function(response) {
$("#myModal").modal('show');
$('#result').html(response);
}
});
});
</script>
这是我通过 AJAX 调用的页面:
$company_name= $_POST['text1'] ;
$api_key = 'xxx'; // Get your API key from here: https://developer.companieshouse.gov.uk
$api = new companiesHouseApi($api_key);
$response = $api->send('/search/companies', ['q' => $company_name]); // Process API request
echo'<style type="text/css">';
echo'.tg {border-collapse:collapse;border-spacing:0;}';
echo'.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}';
echo'.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}';
echo'.tg .tg-yw4l{vertical-align:top}';
echo'</style>';
echo'<table class="tg">';
foreach($response['items'] as $key){
echo' <form action="search.php" method="post">';
echo' <tr>';
echo' <th class="tg-031e">'.$key['title'] . "<br>".'</th>';
echo' <th class="tg-031e" rowspan="3"> <input type="submit" value="Submit"></th>';
echo' </tr>';
echo' <tr>';
echo' <td class="tg-yw4l">'.$key['description'] . "<br>".'</td>';
echo' </tr>';
echo' <tr>';
echo' <td class="tg-yw4l">'.$key['address_snippet'] . "<br>".'</td>';
echo' </tr>';
echo' </form>';
}
echo'</table>';
【问题讨论】: