【发布时间】:2017-12-18 14:32:52
【问题描述】:
我有一个 div,它始终与它旁边的 div 高度相同(取决于该 div 中加载了多少内容等)。
这工作正常,但现在我使用 jquery 添加了一个搜索功能,它停止工作。当我搜索时,再次加载 jquery 计算中使用的元素。所以我尝试在 ajax 回调中添加 jquery 代码,但这也不起作用。我该怎么办?
我的代码:
$(window).resize(function() {
var contenthoogte = $(".news-content").height();
$(".contenthoogteaangepast").css("height", contenthoogte);
}).resize();
我的 HTML:
<div class="col-sm-4 padding-zero contenthoogteaangepast">
<form action="ajax/result.php" id="zoeken">
<div class="zoekveld">
<input class="inputzoek" type="text" placeholder="zoeken..." name="zoekwaarde">
<input class="inputbutton" type="submit" value="Zoeken">
</div>
</form>
<div class="downloads">
<h4>Downloads</h4>
<ul>
<li>
<a href="#">- PDF Voorbeeld 1</a>
</li>
<li>
<a href="#">- PDF Voorbeeld 2</a>
</li>
<li>
<a href="#">- PDF Voorbeeld 3</a>
</li>
</ul>
</div>
</div>
<div class="col-sm-8 padding-zero" id="result">
<div class="box large-width-box-w1 full-width-box news-right entry-content news-content">
<?
if($zoekwaarde != ''){
$zoekresultcon = $conn->query($zoekresult);
while($zoekresult = $zoekresultcon->fetch_assoc()){
$zoekresultaten .= '<div class="zoekresultaat"><h4>'.$zoekresult['title'].'</h4></div>';
}
echo $zoekresultaten;
}else{
?>
<h2 class="port-tit contenttitle"><? echo $content['title']; ?></h2>
<?
//Replace img met img src="cms/ zodat je ook tussen de tekst images kan toevoegen
$replaced = str_replace('img src="','img src="cms/',$content['introtext']);
echo $replaced;
}
?>
</div>
</div>
这是我用 ajax 尝试过的:
$( "#zoeken" ).submit(function( event ) {
// Stop form from submitting normally
event.preventDefault();
// Get some values from elements on the page:
var $form = $( this ),
zoekvalue = $form.find( 'input[name="zoekwaarde"]' ).val(),
url = $form.attr( "action" );
// Send the data using post
var posting = $.post( url, { zoekwaarde: zoekvalue } );
// Put the results in a div
posting.done(function( data ) {
$(window).resize(function() {
var contenthoogte = $(".news-content").height();
$(".contenthoogteaangepast").css("height", contenthoogte);
}).resize();
$("#result").html(data);
});
});
即使不调整屏幕大小也应该改变它,所以我也尝试在 .resize 函数之外复制代码,但这也没有改变任何东西。
【问题讨论】:
-
您在设置
#result内容之前触发了调整大小事件。无论如何,你不应该嵌套事件
标签: javascript jquery css ajax