javascript - 提交后依赖选择标签被清除

标签 javascript php jquery html

问题:

如果我从选择选项中仅选择州,然后单击提交按钮而不选择城市,则会发生城市(选择标签)验证,但为什么城市选择标签选项被清除

帮助我的 friend ,我犯了错误:

<?php

$state=$city="";
$stateErr=$cityErr="";

if ($_SERVER['REQUEST_METHOD']== "POST") {
   $valid = true;

/*State Validation starts here*/     
 if(empty($_POST["parent_selection"]))
{
    $stateErr="*State is Required";
        $valid=false;

}

elseif(empty($_POST["child_selection"]))
    {
        $cityErr="* City is required";
        $valid=false;
    }


    else
    {
     $state=test_input($_POST["parent_selection"]); 
     $city=test_input($_POST["child_selection"]); 
    } 


function test_input($data)
    {
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script language="javascript" type="text/javascript">  
$(document).ready(function(){
    var ArunachalPradesh = [
    {display: "---Please Select---", value: "" }, 
    {display: "Itanagar", value: "Itanagar" }, 
    {display: "Arunachal Pradesh - Other", value: "Arunachal Pradesh - Other" }, 
    ];

var Assam = [
    {display: "---Please Select---", value: "" }, 
    {display: "Guwahati", value: "Guwahati" }, 
    {display: "Silchar", value: "Silchar" }, 
    {display: "Assam - Other", value: "Assam - Other" },
    ];

    $("#parent_selection").change(function() {
        var parent = $(this).val(); //get option value from parent 

        switch(parent){ //using switch compare selected option and populate child
         case 'ArunachalPradesh':
                list(ArunachalPradesh);
                break;              
              case 'Assam':
                list(Assam);
                break;
                 default: //default child option is blank
                $("#child_selection").html('');  
                break;
           }
});
function list(array_list)
{
    $("#child_selection").html(""); //reset child options
    $(array_list).each(function (i) { //populate child options 
        $("#child_selection").append("<option value=\""+array_list[i].value+"\">"+array_list[i].display+"</option>");
    });
}

});
</script>

<title>Untitled Document</title>
</head>

<body>
<form action="select1.php" method="post">

State :<select name="parent_selection" id="parent_selection">
<option label="-- Please Select --"></option>
   <option value="ArunachalPradesh" <?php if(isset($_POST["parent_selection"]) && $_POST["parent_selection"] == "ArunachalPradesh") { ?> selected="selected" <?php }  ?>>ArunachalPradesh</option>
     <option value="Assam" <?php if(isset($_POST["parent_selection"]) && $_POST["parent_selection"] == "Assam") { ?>
        selected="selected" <?php } ?>>Assam</option>
    </select><?php echo $stateErr?><br />
City : <select name="child_selection" id="child_selection">
</select><?php echo $cityErr?><br />
<input type="submit" value="submit"/>

</form>
</body>
</html>

最佳答案

将你的 JavaScript 更改为此

    <script language="javascript" type="text/javascript">  
    $(document).ready(function(){
        var ArunachalPradesh = [
        {display: "---Please Select---", value: "" }, 
        {display: "Itanagar", value: "Itanagar" }, 
        {display: "Arunachal Pradesh - Other", value: "Arunachal Pradesh - Other" }, 
        ];

    var Assam = [
        {display: "---Please Select---", value: "" }, 
        {display: "Guwahati", value: "Guwahati" }, 
        {display: "Silchar", value: "Silchar" }, 
        {display: "Assam - Other", value: "Assam - Other" },
        ];

    jQuery(window).on("load", function(){

      $('#parent_selection').change();

    });

        $("#parent_selection").on('change',function() {
           // var parent = $(this).val(); //get option value from parent 
            var parent = document.getElementById("parent_selection").value;
            switch(parent){ //using switch compare selected option and populate child
             case 'ArunachalPradesh':
                    list(ArunachalPradesh);
                    break;              
                  case 'Assam':
                    list(Assam);
                    break;
                     default: //default child option is blank
                    $("#child_selection").html('');  
                    break;
               }
    });
    function list(array_list)
    {
        $("#child_selection").html(""); //reset child options
        $(array_list).each(function (i) { //populate child options 
            $("#child_selection").append("<option value=\""+array_list[i].value+"\">"+array_list[i].display+"</option>");
        });
    }

    });

    </script>

关于javascript - 提交后依赖选择标签被清除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21772666/

相关文章:

php - 搜索栏不会返回结果

php - 必须更改临时密码

javascript - 基金会 4 : Orbit: Initialitation after page loading & Ajax: Reinitialization

javascript - 获取选择框值以用作变量

jquery - 刺耳的切换效果

javascript - 面积图中的填充颜色堆叠

javascript - Highcharts 条形图将数据标签格式化为百分比并添加文本

javascript - 包括 JQuery 库

javascript - 访问 id 中的标题

php - 如何在使用 echo 时从 HTML 转义?