从PHP的下拉列表中获取选定的文本 [英] Get selected text from dropdown in PHP

查看:243
本文介绍了从PHP的下拉列表中获取选定的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP完全陌生,实际上我这样做的原因是自定义wordpress插件,以便它可以满足我的需求.到目前为止,我有一个默认表单,我现在正在做的是添加国家/地区下拉列表.这是我的添加方式

I am totally new in PHP, in fact the reason I am doing this is to customize a wordpress plugin so it can fit my need. So far I have a default form, what I am doing now is to add a country dropdown. Here's how I add it

<div class="control-group">
    <label class="control-label" for="Country">Country :</label>
    <div class="controls">
        <select id="itemType_id" name="cscf[country]" class="input-xlarge">
            <option value="malaysia@email.com">Malaysia</option>
            <option value="indonesia@email.com">Indonesia</option> 
        </select>   
        <span class="help-inline"></span>
    </div>  
</div>

到目前为止,我只能使用

So far I only able to retrieve the value of selected item with

$cscf['country'];

在这种情况下,如何获取显示文字(即国家/地区名称)?

How can I get the display text which is the country name in this case ?

推荐答案

您可以使用隐藏字段,并且当下拉列表的选定值发生更改时,可以使用JavaScript和jQuery将值设置为此字段.

You can use a hidden field, and with JavaScript and jQuery you can set the value to this field, when the selected value of your dropdown changes.

<select id="itemType_id" name="cscf[country]" class="input-xlarge">
  <option value="malaysia@email.com">Malaysia</option>
  <option value="indonesia@email.com">Indonesia</option> 
</select>
<input type="hidden" name="country" id="country_hidden">

<script>
  $(document).ready(function() {
    $("#itemType_id").change(function(){
      $("#country_hidden").val(("#itemType_id").find(":selected").text());
    });
  });
</script>

然后,在提交页面时,您可以使用来获取国家名称

Then when your page is submitted, you can get the name of the country by using

$_POST["country"]

这篇关于从PHP的下拉列表中获取选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆