因此,当我为第一个选项选择一个值时,它将更改第二个选项的值。 这是 getSkill-6ren">
gpt4 book ai didi

php - 如何将更改应用到动态表单

转载 作者:行者123 更新时间:2023-11-29 11:40:01 26 4
gpt4 key购买 nike

我有一个由用户选择生成的表单

这是代码:

<select id="skillcat" name="skillcat[]" class="form-control" onChange="getSkillDescription(this.value)">
<option value ="">SELECT SKILL CATEGORY</option>
<?php
foreach($results as $displayCategory) { ?>
<option value="<?php echo $displayCategory["SkillCatID"]; ?>"><?php echo $displayCategory["SkillCatName"]; ?></option>
<?php } ?>
</select>
<i class="arrow"></i>

<select id="skilldes" name="skilldes[]" class="form-control"></select>
<i class="arrow"></i>

因此,当我为第一个选项选择一个值时,它将更改第二个选项的值。

这是 getSkillDescription() 的 JavaScript

<script type="text/javascript">
function getSkillDescription(val) {
$.ajax({
type: "POST",
url: "get_skilldescription.php",
data:'skill_id='+val,
success: function(data){
$("#skilldes").html(data);
}});}
</script>

脚本将前往 get_skilldescription.php。下面是代码:

<?php
include("include/dbconn.php");
session_start();
$kp = $_SESSION["id"];
function runQuery($query) {
$result = mysql_query($query);
while($row=mysql_fetch_assoc($result)) {
$resultset[] = $row;
}
if(!empty($resultset))
return $resultset;
}

if(!empty($_POST["skill_id"])) {

$skill=$_POST["skill_id"];

switch($skill)
{
case 1: $query ="SELECT * FROM skill WHERE skillCatID = 1";
$results = runQuery($query); break;
case 2: $query ="SELECT * FROM skill WHERE skillCatID = 2";
$results = runQuery($query); break;
case 3: $query ="SELECT * FROM skill WHERE skillCatID = 3";
$results = runQuery($query); break;
case 4: $query ="SELECT * FROM skill WHERE skillCatID = 4";
$results = runQuery($query); break;
case 5: $query ="SELECT * FROM skill WHERE skillCatID = 5";
$results = runQuery($query); break;
case 6: $query ="SELECT * FROM skill WHERE skillCatID = 6";
$results = runQuery($query); break;
case 7: $query ="SELECT * FROM skill WHERE skillCatID = 7";
$results = runQuery($query); break;
case 8: $query ="SELECT * FROM skill WHERE skillCatID = 8";
$results = runQuery($query); break;
case 9: $query ="SELECT * FROM skill WHERE skillCatID = 9";
$results = runQuery($query); break;
case 10: $query ="SELECT * FROM skill WHERE skillCatID = 10";
$results = runQuery($query); break;
case 11: $query ="SELECT * FROM skill WHERE skillCatID = 11";
$results = runQuery($query); break;
case 12: $query ="SELECT * FROM skill WHERE skillCatID = 12";
$results = runQuery($query); break;
case 13: $query ="SELECT * FROM skill WHERE skillCatID = 13";
$results = runQuery($query); break;
case 14: $query ="SELECT * FROM skill WHERE skillCatID = 14";
$results = runQuery($query); break;
case 15: $query ="SELECT * FROM skill WHERE skillCatID = 15";
$results = runQuery($query); break;
case 16: $query ="SELECT * FROM skill WHERE skillCatID = 16";
$results = runQuery($query); break;
case 17: $query ="SELECT * FROM skill WHERE skillCatID = 17";
$results = runQuery($query); break;
case 18: $query ="SELECT * FROM skill WHERE skillCatID = 18";
$results = runQuery($query); break;


}
?>
<option value="">Select Skill</option>
<?php
foreach($results as $displayskill) {
?>
<option value="<?php echo $displayskill["SkillID"]; ?>"><?php echo $displayskill["SkillDescription"]; ?></option>
<?php
}
}
?>

现在的问题是,当我添加一个新表单时,它会创建另外两个选项。但是,当我更改新创建的表单的第一个选项时,它将更改第一个表单的选项值,而不是新创建的表单。我希望你们能帮我解决这个问题。谢谢

最佳答案

您正在添加一个新表单,其中的元素具有与第一个表单相同的 id。

ID 是唯一的,请确保每个表单都有具有唯一 ID 的字段。

此外,您再次调用 getSkillDescription ,它显式设置第一个选择选项 $("#skilldes") 因此您需要在传递您将要填充的选择的 id。

function getSkillDescription(val, selectID) {
$.ajax({
type: "POST",
url: "get_skilldescription.php",
data:'skill_id='+val,
success: function(data){
$("#" + selectID).html(data);
}
});
}

关于php - 如何将更改应用到动态表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35957189/

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