从PHP下拉列表中获取所选文本

abi*_*eez 3 html php forms wordpress

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

<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>
Run Code Online (Sandbox Code Playgroud)

到目前为止,我只能检索所选项目的值

$cscf['country'];
Run Code Online (Sandbox Code Playgroud)

在这种情况下,如何获取国家/地区名称的显示文本?

eye*_*tea 11

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>
Run Code Online (Sandbox Code Playgroud)

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

$_POST["country"]
Run Code Online (Sandbox Code Playgroud)

  • 我认为这是迄今为止最简单的方法. (3认同)