我创建了州和城市的依赖性下拉。这是财产提交表格。当用户编辑表单时,我想显示所选的州和城市。我设法以某种方式显示选定的状态。我写了get_city函数function.php并通过ajax调用它。我有两个不同的表,即州和城市。所以问题是我不明白如何显示选定的城市? 这是我的下拉代码
<select name="state" id="state" class="select-submit2">
<option value="">Select state</option>
<?php
$property_id = $_GET['listing_edit'];
$property_state = get_post_meta($property_id, state, true);
$result=$wpdb->get_results("select * from states");
//$wpdb->get_results($query);
foreach($result as $row) {
$state_id=$row->state_id;
$state_name=$row->state_name;
?>
<option value="<?php echo $state_id; ?>" <?php if($property_state == $state_id){ echo
'selected="selected"';} ?>><?php echo $state_name; ?></option>
<?php
//echo '<option value='.$state_id.'>'.$state_name.'</option>';
}
?>
</select>
<input type="hidden" name="edit_id" id="edit_id" value="<?php echo $edit_id;?>">
<select name="district" id="district" required class="select-submit2">
<option value="">Select District</option>
</select>
在上面的代码中我显示了选中的状态。我在google上搜索,发现我必须将post id传递给function.php中的函数。这是我在function.php文件中的get_city函数的代码
function getcity() {
global $wpdb;
if($_POST['state']) {
$id = $_POST['state'];
global $edit_id;
$property_id = $edit_id;
$district = get_post_meta($property_id, 'district', true);
$result=$wpdb->get_results("SELECT * FROM districts WHERE state_id='$id'");
foreach($result as $row) {
$city_name = $row->district_name;
$city_id = $row->district_id;
?>
<option value="<?php echo $city_id; ?>" <?php if($district == $city_id){ echo 'selected="selected"';} ?>><?php echo $city_name; ?></option>
<?php
//echo '<option value="'.$city_id.'">'.$city_name.'</option>';
}
}
}
add_action("wp_ajax_nopriv_getcity", "getcity");
add_action("wp_ajax_getcity", "getcity");
这是我的js代码......
jQuery(document).ready(function($) {
$('#state').on('change', function() {
var state = $('#state').val();
var edit_id = $('#edit_id').val();
$.ajax
({
type : "POST",
url : "http://whitecode.in/demo/plotsup_plot/wp-admin/admin-ajax.php",
data :{'action' : 'getcity', 'state' : state , 'edit_id' : edit_id },
success: function(html) {
$("#district").removeAttr("disabled");
$("#district").html(html);
}
});
});
<input type="hidden" name="edit_id" id="edit_id" value="<?php echo $edit_id;?>">
答案 0 :(得分:0)
您必须将上述代码放在一个函数中。让函数名称为myFunction()
。
如果要将post id传递给函数,请调用post id作为参数的函数,即myFunction($postid)
其中$postid
是post id。
请不要忘记从post循环调用函数,以便函数调用每个postid。
答案 1 :(得分:0)
我个人为每个州加载a,其中style =&#34; display:none;&#34;,之后,当用户选择状态时,相应的更改样式=&#34; display:none;&#34; to style =&#34; display:inline;&#34;
答案 2 :(得分:0)
试试这个,让我知道它是否有效。更改是在两行全局$ edit_id上进行的;已删除,$ property_id行已更改。请正确应用更改并告知我们是否正常。
<?php
function getcity() {
global $wpdb;
if($_POST['state']) {
$id = $_POST['state'];
$property_id = $_POST['edit_id'];
$district = get_post_meta($property_id, 'district', true);
$result=$wpdb->get_results("SELECT * FROM districts WHERE state_id='$id'");
foreach($result as $row) {
$city_name = $row->district_name;
$city_id = $row->district_id;
?>
<option value="<?php echo $city_id; ?>" <?php if($district == $city_id){ echo 'selected="selected"';} ?>><?php echo $city_name; ?></option>
<?php
//echo '<option value="'.$city_id.'">'.$city_name.'</option>';
}
}
}
add_action("wp_ajax_nopriv_getcity", "getcity");
add_action("wp_ajax_getcity", "getcity");