我有一个包含公司名称地址邮编等多个AJAX请求,而无需刷新
行我有一个选择:如果你不知道的全部细节,你可以输入名称,然后单击搜索的形式。这将通过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>';
我仍然困惑对不起引用它们在你的JavaScript代码。该模式显示Ajax调用的结果。然后我需要关闭模式并填充原始字段而不刷新。 –
由于模态是在第一个页面代码中,所以您可以在点击模式中的OK按钮时运行javascript函数。代码将采用模态字段的值,然后在主页面中设置字段的值,最后关闭模态。 (这些字段需要唯一的ID以确保将数据放入正确的字段中)。这可以在不刷新页面的情况下运行,因为get或post的任何内容都需要刷新页面。 – NoLiver92
谢谢你的工作我快到了。我设法填充字段但是由于某种原因,无论我按下哪个按钮,只传递第一个按钮的值,即使每个表单中的值都不相同? –