当用户更改其选择时,使用PHP和jQuery更新页面中的标记
每当用户在selecthtml标记中更改其选择时,我需要更新一个post图像 为了做到这一点,我正在尝试使用jQuery和php 我需要获取用户选择的选项的id 然后我想使用这个id使用wordpress函数获取图像url 最后,我想把这个url添加到一个图像标签中 这是我的代码:当用户更改其选择时,使用PHP和jQuery更新页面中的标记,php,jquery,ajax,var,Php,Jquery,Ajax,Var,每当用户在selecthtml标记中更改其选择时,我需要更新一个post图像 为了做到这一点,我正在尝试使用jQuery和php 我需要获取用户选择的选项的id 然后我想使用这个id使用wordpress函数获取图像url 最后,我想把这个url添加到一个图像标签中 这是我的代码: <script type="text/javascript"> $(function(){ $('#select2').select2(); $('#select
<script type="text/javascript">
$(function(){
$('#select2').select2();
$('#select2').on('change', function() {
var country = $('#select2 option:selected').val();
<?php
$the_id = 'I NEED HERE ID BY SELCETING IN DROPDPWN';
$url=wp_get_attachment_url(get_post_thumbnail_id($the_id));
?>
$(".countryvalue").html('<img width="80px" src="<?php echo $url; ?>" />');
});
});
</script>
<div class="countryvalue" ></div>
<select id="select2" name="marv_the_author">
<option value="0">--Select author--</option>
<option value="100">test</option>
<option value="101">eeeee</option>
</select>
为此,您应该使用ajax。 更改jquery函数以发送ajax调用, 并创建一个php页面,用于处理该调用并发回正确的url jquery函数注意,此页面上没有php脚本,您可以将其保留为一个简单的HTML页面:
$(document).ready(function(){
$('#select2').on('change', function() {
var country = $('#select2 option:selected').val();
$.ajax({url: "getImageUrl.php?id=" + country,
success: function(result){
$(".countryvalue").html('<img width="80px" src="' + result + '" />');
}});
});
});
创建不同的php页面,如下所示:
<?php
$id = $_REQUEST["id"];
$url=wp_get_attachment_url(get_post_thumbnail_id($id));
echo $url;
您应该使用ajax,谢谢您的回答,但我不需要重新加载页面,我希望在同一页面中显示图像这就是ajax的功能,我建议您阅读这篇文章。检查此链接:和此:。ajax不会再次重新加载页面。它只通过动态调用其他页面来更改页面内容。我已修复jquery脚本,请重试。如果不起作用,请尝试以下步骤,并让我知道堆栈:1。浏览至:http://your_local_host/getImageUrl.php?id=123 . 页面上有123吗?。2.var后国家=$。。。添加此行:alertcountry;console.logcountry;。你有什么发现吗?。3.将success:functionresult body从:$.countryvalue.html更改为:;致:结果;console.logresult;。你得到什么了吗?我尝试了:1-是,在我添加这个echo$id=$\u请求[id]。。。。。2什么也得不到。。。。。我什么也得不到。。。。感谢如果我删除这个$.ajax{url:getImageUrl.php?id=+country,成功:functionresult{$.countryvalue.html;alertresult;console.logresult;};}。。。。。。在警报中执行第2点。。