我有一个表单,我想将结果加载到div中。我已经搜索了一些关于这个的主题,我认为这个jquery submit form and then show results in an existing div会起作用,但事实并非如此。
到目前为止,这是我的代码:
<script type="text/javascript"> $('#form').submit(function() { // catch the form's submit event
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // get the form data
type: $(this).attr('method'), // GET or POST
url: $(this).attr('action'), // the file to call
success: function(response) { // on success..
$('#test').html(response); // update the DIV
}
});
return false; // cancel original event to prevent form submitting }); </script>
<DIV id="test"></DIV>
<FORM id="form" name="pcc" method="post" action="http://wl.breedbandwinkel.nl/postcodecheck" onSubmit="return validatePcc(this);">
<div class="one_third firstcols">
<H4>Ik ben op zoek naar:</H4>
<DIV class="ff"><INPUT type="radio" class="pccrad radio" name="sub" value="alles-in-een-pakketten" id="pcc-alles-in-een-pakketten" onclick="$('#pcc-no').hide(); $('#pcc-fpcon').css('visibility', 'visible'); if($('#pcg').val() == '') $('#pcg').focus();"><LABEL class="left pcm" for="pcc-alles-in-een-pakketten" onmouseover="mpopup('Alles-in-één pakketten','Extra voordelig pakket met internet, digitale telefonie en/of digitale televisie.');" onmouseout="kill();">Alles-in-één pakketten</LABEL></DIV> <DIV class="ff"><INPUT type="radio" class="pccrad radio" name="sub" value="internet" id="pcc-internet" onclick="$('#pcc-no').hide(); $('#pcc-fpcon').css('visibility', 'visible'); if($('#pcg').val() == '') $('#pcg').focus();"><LABEL class="left pcm" for="pcc-internet" onmouseover="mpopup('Internet','Altijd supersnel onbeperkt online tegen een vast bedrag per maand.');" onmouseout="kill();">Internet</LABEL></DIV> <DIV class="ff"><INPUT type="radio" class="pccrad radio" name="sub" value="digitale-televisie" id="pcc-digitale-televisie" onclick="$('#pcc-no').hide(); $('#pcc-fpcon').css('visibility', 'visible'); if($('#pcg').val() == '') $('#pcg').focus();"><LABEL class="left pcm" for="pcc-digitale-televisie" onmouseover="mpopup('Digitale Televisie','Geniet van haarscherp digitaal beeld en geluid inclusief de gratis digitale programmagids.');" onmouseout="kill();">Digitale Televisie</LABEL></DIV>
</div><!-- end .one_third -->
<div class="one_third">
<H4>Mijn postcode en huisnummer zijn:</H4>
<TABLE border="0" cellspacing="0" cellpadding="0">
<TR>
<TD height="14" colspan="2">Postcode</TD>
<TD>Huisnr.</TD>
</TR>
<TR>
<TD width="51"><INPUT type="text" class="text" maxlength="4" size="5" value="" name="pcg" id="pcg" onKeyUp="autoTab(event,this,4,pcl);" onFocus="chBg(pcc,'pcg');" onBlur="chBg(pcc,'reset');" style="width: 41px;"></TD>
<TD width="46"><INPUT type="text" class="text" maxlength="2" size="2" value="" name="pcl" id="pcl" onKeyUp="autoTab(event,this,2,hn);" onKeyDown="backSpace(event,this,pcg);" onFocus="chBg(pcc,'pcl');" onBlur="chBg(pcc,'reset'); upperCase(event,this);" style="width: 26px;"></TD>
<TD width="36"><INPUT type="text" class="text" maxlength="6" size="4" value="" name="hn" id="hn" onKeyDown="backSpace(event,this,pcl);" onFocus="chBg(pcc,'hn');" onBlur="chBg(pcc,'reset');" style="width: 36px;"></TD>
</TR>
</TABLE>
<U class="dot small" onmouseover="popup('Waarom mijn postcode invullen?','Om te kunnen controleren welke abonnementen op uw adres leverbaar zijn hebben wij uw postcode en huisnummer nodig.<br>Uiteraard respecteren wij uw privacy. Deze gegevens worden niet opgeslagen.');" onmouseout="kill();">
Waarom mijn postcode invullen?</U>
</div><!-- end .one_third -->
<div class="one_third lastcols">
<INPUT type="submit" name="submit" value="Vergelijk de aanbiedingen op uw adres" class="button">
</div><!-- end .one_third -->
</FORM>
所以我发现这不起作用。我真的想将动作网址(http://wl.breedbandwinkel.nl/postcodecheck)的结果显示为<div id="test">
。
目前我在iframe中这样做但是看起来并不“专业”。
我希望我能提供足够的信息,如果不是让我们知道的话。
答案 0 :(得分:1)
最重要的是,如果您的网页未在http://wl.breedbandwinkel.nl内运行,则由于大多数浏览器都禁用跨站点脚本,因此AJAX调用可能根本无法运行。您无法从bar.com提供的页面向foo.com发出AJAX请求。为了避免这种情况,我通常会写一个文件,如“bar.com/ajaxActions.php”,然后使用该PHP脚本向外部站点发出GET或POST请求。一旦它检索到结果,我只是将结果打印到标准输出,然后该输出成为您的AJAX请求的结果。
我要做的另一件事是抛弃你的<form>
标签中的“方法”和“动作”属性,并将这些属性放入你试图用jQuery编写的AJAX请求中。请改为使用提交按钮中的onClick()侦听器:
<INPUT type="submit" name="submit" onClick="doAjaxRequest()" value="Vergelijk de aanbiedingen op uw adres" class="button">
然后在该功能中,使用您已经为初学者准备的代码:
<script type="text/javascript">
function doAjaxRequest() {
$.ajax({ // create an AJAX call...
data: $("#form").serialize(), // get the form data
type: GET, // GET or POST
url: "ajaxActions.php", // the file to call -- postcodecheck if you're on that same domain, otherwise ajaxActions.php
success: function(response) { // on success..
$('#test').html(response); // update the DIV
}
});
}
最后,如果您不在同一个域中,我将留给您查找GET请求的PHP代码(使用file_get_contents())或POST请求(使用cURL)。或者,如果您不能使用PHP,那么请使用您选择的后端语言。在PHP中,假设您的邮政编码检查程序接受了GET请求,您的ajaxActions.php文件将如下所示:
$response = file_get_contents("http://wl.breedbandwinkel.nl/postcodecheck?pcg=" . $_GET["pcg"] . "&pcl=" . $_GET["pcl"] . "&hn=" . $_GET["hn"] );
print $reponse;