简体   繁体   English

在Wordpress中如何使用多种形式选择添加和显示数据?

[英]In Wordpress how to use multiple form select to add and show a data?

I am using to select menu to select Country-> State-> Area to show a Map. 我正在使用选择菜单来选择“国家”->“州”->“区域”以显示地图。

if selecting a Country > State to be change dynamical and alse Area. 如果选择“国家/地区”>“要更改动态和其他区域的州”。

how to add the data in wordpress. 如何在WordPress中添加数据。 example I need to add a country state and area and to be link together. 示例我需要添加一个国家/地区和区域并将其链接在一起。

ex: 例如:

country : india, singapore. 国家:印度,新加坡。

state : india>tamilnadu 州:印度>泰米尔纳德邦

city : india> tamilndu> chennai. 城市:印度>塔米尔杜>钦奈。

how to add this in wordpress and how to set parent? 如何在WordPress中添加这个以及如何设置父?

To make this you have 2 options. 为此,您有2个选项。

Use only the frontend. 仅使用前端。 Use the database. 使用数据库。

To use the frontend only you might do something like this 要仅使用前端,您可以执行以下操作

<select name="select1" id="select1">
    <option value="1">Fruit</option>
    <option value="2">Animal</option>
    <option value="3">Bird</option>
    <option value="4">Car</option>
</select>


<select name="select2" id="select2">
    <option value="1">Banana</option>
    <option value="1">Apple</option>
    <option value="1">Orange</option>
    <option value="2">Wolf</option>
    <option value="2">Fox</option>
    <option value="2">Bear</option>
    <option value="3">Eagle</option>
    <option value="3">Hawk</option>
    <option value="4">BWM<option>
 </select>

<input id="show-map" value="show-map" />

and at your javascript part 在你的javascript部分

$("#select1").change(function() { 
if($(this).data('options') == undefined){
    /*Taking an array of all options-2 and kind of embedding it on the select1*/
    $(this).data('options',$('#select2 option').clone());
    } 
var id = $(this).val();
var options = $(this).data('options').filter('[value=' + id + ']');
$('#select2').html(options);
});

$("#show-map").click(function(){
    //here you capture the input values and do what you need to do
});

So, the other option is using DB, this one is more complex, but basically you need to do the same, but getting the select values with an ajax. 因此,另一种选择是使用DB,这比较复杂,但是基本上您需要这样做,但是要用ajax获取选择值。

To make an ajax in wordpress is quite simple, just read about it here 在wordpress中制作ajax非常简单,只需在此处阅读

https://premium.wpmudev.org/blog/using-ajax-with-wordpress/ https://premium.wpmudev.org/blog/using-ajax-with-wordpress/

It will let you invoke the ajax function that you will set inside the .change function , like this 它将允许您调用将在.change函数内部设置的ajax函数,如下所示

 $("#select1").change(function() { 
   //here you might want to get the value of this select to later associate it with your database, so do something like $(this).val();
$.ajax({url: "/your-url", success: function(result){
        $("#states").html(result);
    }});  
//you might get the select as a result of your ajax.. it should be easy to be done
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM