gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 01:33:52 25 4
gpt4 key购买 nike

问题:

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

帮助我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com