gpt4 book ai didi

php - 动态下拉列表填充文本框

转载 作者:行者123 更新时间:2023-11-30 23:25:57 27 4
gpt4 key购买 nike

我上下搜索了此总线的解决方案,但未能解决。当我想要一个动态下拉菜单来调整第二个下拉菜单中的值并在文本框中填写文本值时,我设法让它工作。

现在我要删掉第二步:即。我实际上想摆脱第二个下拉列表,只需在文本框中输入一个值。我已尽一切努力删除第二步,但一旦我这样做,一切都停止了。

目前函数查看第二个下拉菜单并为其设置选项,然后我添加了行

document.getElementById('fld_ClientID').value =""

让它在文本框中输入数据。我如何完全摆脱对 tblPromotions 的引用并使其仅获取文本框的数据。

<script language="javascript">  
function setOptions(chosen) {
var selbox = document.myform.selectpromotion;

selbox.options.length = 0;
if (chosen == "0") {
selbox.options[selbox.options.length] = new Option('First select a client','0');

}
<?php
$client_result = mysql_query("SELECT * FROM tblClients ORDER BY ClientName") or die(mysql_error());
while(@($c=mysql_fetch_array($client_result)))
{
?>
if (chosen == "<?=$c['ClientID'];?>") {

<?php
$c_id = $c['ClientID'];
$promo_result = mysql_query("SELECT * FROM tblPromotions WHERE ClientID='$c_id'") or die(mysql_error());
while(@($m=mysql_fetch_array($promo_result)))
{
?>
selbox.options[selbox.options.length] = new
Option('<?=$m['PromotionName'];?>','<?=$m['ClientID'];?>');
document.getElementById('fld_ClientID').value ="<?=$m['ClientID'];?>";
<?php
}
?>
}
<?php
}
?>
}
</script>
</head>
<body>
<form name="myform" method="POST" action="processaddpromotionNEW.php"> ><div align="center">
<p>
<select name="selectclient" size="1"
onchange="setOptions(document.myform.selectclient.options
[document.myform.selectclient.selectedIndex].value);">
<option value="0" selected>Select a client</option>
<?php
$result = mysql_query("SELECT * FROM tblClients ORDER BY ClientName") or die(mysql_error());
while(@($r=mysql_fetch_array($result)))
{
?>
<option value="<?=$r['ClientID'];?>">
<?=$r['ClientName'];?>
</option>
<?php
}
?>
</select>
<br><br>
<select name="selectpromotion" size="1">
<option value=" " selected>First select a client</option>
</select>
</p>
<p>
<input name="fld_ClientID" type="text" class="Arial" id="fld_ClientID" tabindex="11" size="10" />
<br>

最佳答案

也许,您不应该在 javascript 函数中执行 mysql 查询。你应该: 使用 ajax 获取可能的选项 或者 一次查询所有可能的选项,然后将其保存在全局变量中

类似于:

<script>
var secondOptions = {};
<?php
$promo_result = mysql_query("SELECT * FROM tblPromotions") or die(mysql_error());
while(@($m=mysql_fetch_array($promo_result))){
?>
if(secondOptions['<?=$m['ClientID'];?>'] == undefined)
secondOptions['<?=$m['ClientID'];?>'] = {};
secondOptions['<?=$m['ClientID'];?>']['<?=$m['PromotionId'];?>'] = '<?=$m['PromotionName'];?>';
<?php
}
?>

setOptions(clientId){
jQuery("select[name=selectpromotion]").empty();
options = "";
jQuery.each(secondOptions[clientID],function(a,b){
options += "<option value='"+a+"'>"+b+"</options>";
});
jQuery("select[name=selectpromotion]").append(options);
}

<select name="selectclient" size="1" onchange="setOptions(jQuery(this).val());">

...

关于php - 动态下拉列表填充文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13339983/

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