【问题标题】:This form doesn't update the user selection at the drop down list when submitting the form through PHP通过 PHP 提交表单时,此表单不会更新下拉列表中的用户选择
【发布时间】:2011-06-29 14:44:22
【问题描述】:
<!DOCTYPE html>
<html>
    <script type="text/javascript"> 
            function updateSelectTarget () {
                var id = this.options[this.selectedIndex].value;
                var targets = this.parentNode.getElementsByTagName("select");
                var len = targets.length;
                for (var i = len - 1; i > 0; --i) {
                    if (targets[i].id == id) {
                        targets[i].style.display = "block";
                    }
                    else {
                        targets[i].style.display = "none";
                    }
                }
            }
            function initChangeHandlers () {
                var i, el;
                var allSelectElements = document.getElementById("myform").getElementsByTagName("select");
                for (i in allSelectElements) {
                    el = allSelectElements[i];
                    if (el.className == "changeable") {
                        el.onchange = updateSelectTarget;
                        el.onchange();
                    }
                }
            }
            window.onload = initChangeHandlers;
    </script> 
<head>

</head>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="myform" id="myform">

<fieldset>
    <legend>Location</legend> 
    <select id="country" class="changeable" name="country"> 
        <option value="England">England</option> 
        <option value="France">France</option> 
        <option value="Germany">Germany</option> 
    </select>   
    <hr/>
    <select id="England" name="city"> 
        <option value="Birmingham">Birmingham</option> 
        <option value="Liverpool">Liverpool</option> 
        <option value="London">London</option> 
    </select> 
    <select id="France" class="hidden" name="city"> 
        <option value="Lyon">Lyon</option> 
        <option value="Marseille">Marseille</option> 
        <option value="Paris">Paris</option> 
    </select> 
    <select id="Germany" class="hidden" name="city"> 
        <option value="Berlin">Berlin</option> 
        <option value="Hamburg">Hamburg</option> 
        <option value="Munich">Munich</option> 
    </select> 
</fieldset>

<input type="submit" name="submit" value="submit">

    <?php  
    echo "The selection country is $country and the city is $city"; 
    ?> 

</form>

</body>
</html>

【问题讨论】:

  • 您是否尝试将&lt;script&gt; 标签放在&lt;head&gt; 中?

标签: php html forms xhtml


【解决方案1】:

要修复您的代码,请尝试在回发时设置值:

<select id="country" class="changeable" name="country" id="country"> 
    <option value="England">England</option> 
    <option value="France">France</option> 
    <option value="Germany">Germany</option> 
</select>
<?php if(isset($_POST['country'])){ ?>
<script type="text/javascript">
 document.getElementById('country').value = '<?php echo $_POST['country']; ?>';
</script>
<?php } ?>

【讨论】:

  • 我建议你使用流行的 javascript 框架,比如 jQuery,它可以简化选择元素和所有循环。
  • 上述建议对我不起作用。更新国家变量没有问题。问题出在“城市”变量上。
  • @udaysubedi 所以请在您的问题中说明这一点。我们无法猜测您的问题是什么。您有多个带有name="city" 的元素,它们将相互重叠。您可以禁用隐藏的下拉菜单,这将导致仅提交可见的下拉菜单。
  • 目标是传递相应国家/地区的相应城市变量。这是在 Web 服务器上实现的链接。这将清楚地解释问题。 newtext.me/cityval.php
猜你喜欢
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-31
  • 2014-08-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多