【问题标题】:Dependant select disappears after submitting [closed]提交后依赖选择消失[关闭]
【发布时间】:2013-01-20 07:19:24
【问题描述】:

我有一个显示列表的搜索表单。

In the form there is a dependant select for the cities (when a country is selected the select for the cities is shown).

问题:当我提交表单时,页面被刷新,所以城市的选择消失了。我应该怎么做才能在推送提交后保持该选择?我应该使用 ajax 提交表单吗?

【问题讨论】:

  • 你一开始是用PHP来生成列表的吗?

标签: php jquery forms web-applications


【解决方案1】:

您需要检查选择框的postget 值并为该特定值设置SELECTED

<?php @$selected[$_GET['test']] = 'SELECTED'; ?>
<form>
  <select name="test">
    <option value="a" <?=@$selected['a']?>>1</option>
    <option value="b" <?=@$selected['b']?>>2</option>
    <option value="c" <?=@$selected['c']?>>3</option>
    <option value="d" <?=@$selected['d']?>>4</option>
  </select>
  <input type="submit" />
</form>

【讨论】:

    【解决方案2】:

    if($_POST["county"]!=""){"load your cities drop down. and city drop down will be filtered by county drop down."} 一样进行检查。认为它会帮助你。

    【讨论】:

      【解决方案3】:

      选择 1 -> 国家

      选择 2 -> 一个国家的城市。我假设选项是使用 AJAX 加载的

      您可以使用$_POSTselected="selected" 从Select 1 中选择选项,即使它们存在于$_POST 中,也无法选择Select 2 值。

      您必须为此使用 AJAX 表单提交。

      【讨论】:

        【解决方案4】:

        首先您需要保存列表中的值,假设它是$_POST['city']。然后,如果您在同一页面上并重新生成相同的城市列表,假设代码类似于:

        foreach ($cities as $city)
        {
            echo "<option value='{$city}'>{$city}</option>";
        }
        

        改成:

        foreach ($cities as $city)
        {
            echo "<option value='{$city}'";
            if ($city == $_POST['city']) { echo ' selected="yes"'; }
            echo ">{$city}</option>";
        }
        

        如果我们不是在谈论同一个页面,那么一旦你得到$_POST['city'],就把它保存到一个会话变量中,例如$_SESSION['city'],然后像上面一样使用。这也可以扩展到国家等。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-04
          相关资源
          最近更新 更多