반응형
다음과 같이 '프로젝트'테이블이 있습니다.
Project_ID | Client_ID
---------------------------
P1 C1
P2 C1
P3 C2
P4 C2
P5 C3
P6 C3
모든 클라이언트 ID를 표시하는 select id = "1"이 있습니다.
클라이언트 ID에 할당 된 모든 프로젝트를 표시하기 위해 다른 select id = "2"를 표시하고 있습니다.
기본적으로 select id = "1"에서 선택한 값을 기반으로 select id = "2"에 대한 옵션 값을 표시하고 싶습니다.
어떤 사람이 선택하는 경우의 예
<option value="C3">C3</option>
그럼 아래에 있어야합니다
<select id="2">
<option value="P5">P5</option>
<option value="P6">P6</option>
</select>
Ajax가 답이라고 생각하지만 적절한 방법을 모르겠습니다.
해결 방법
선택 상자의 onChange 이벤트에 대한 이벤트 리스너를 추가 할 수 있습니다. On the change event get the value of the select box and send its value to the server using an ajax request and fetch the value you want to show in the second select box based on the first one's value and show it in the second select box. 국가 선택에 따른 주 선택을위한 예제 코드 :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Populate City Dropdown Using jQuery Ajax</title>
<script type="text/javascript" src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("select.country").change(function(){
var selectedCountry = $(".country option:selected").val();
$.ajax({
type: "POST",
url: "process-request.php",
data: { country : selectedCountry }
}).done(function(data){
$("#response").html(data);
});
});
});
</script>
</head>
<body>
<form>
<table>
<tr>
<td>
<label>Country:</label>
<select class="country">
<option>Select</option>
<option value="usa">United States</option>
<option value="india">India</option>
<option value="uk">United Kingdom</option>
</select>
</td>
<td id="response">
<!--Response will be inserted here-->
</td>
</tr>
</table>
</form>
</body>
</html>
백엔드 :
<?php
if(isset($_POST["country"])){
// Capture selected country
$country = $_POST["country"];
// Define country and city array
$countryArr = array(
"usa" => array("New Yourk", "Los Angeles", "California"),
"india" => array("Mumbai", "New Delhi", "Bangalore"),
"uk" => array("London", "Manchester", "Liverpool")
);
// Display city dropdown based on country name
if($country !== 'Select'){
echo "<label>City:</label>";
echo "<select>";
foreach($countryArr[$country] as $value){
echo "<option>". $value . "</option>";
}
echo "</select>";
}
}
?>
참조 페이지 https://stackoverflow.com/questions/42524122
반응형
'MySql' 카테고리의 다른 글
MySQL-InnoDB 대 MyISAM (0) | 2020.10.28 |
---|---|
MySQL을 Python 3.6과 연결 (0) | 2020.10.28 |
MySQL Laravel에서 TEMPORARY 테이블을 만드는 방법 (0) | 2020.10.28 |
MySQL mysql 데이터베이스에 html을 저장하는 방법 (0) | 2020.10.27 |
MySQL Laravel Sum 열 데이터베이스 Eloquent (0) | 2020.10.27 |
댓글