[英]Dynamic drop down list to fill in a text box
I have searched high and low for a resolution to this bus have not been able to work it out. 我搜索了高低寻找此总线的分辨率,但无法解决。 I managed to get this to work when I wanted a dynamic drop down to adjust the values in a second drop down and fill in a text value in a text box.
当我想要动态下拉菜单以在第二个下拉菜单中调整值并在文本框中填写文本值时,我设法使它起作用。
Now I want to cut out the second step: ie. 现在,我想删除第二步:即。 I actually want to get rid of the second drop down and simply enter a value in the text box.
我实际上想摆脱第二个下拉菜单,只需在文本框中输入一个值。 I have tried everything to remove the second step but as soon as I do everything stops working.
我已经尝试了所有步骤以删除第二步,但是一旦完成,一切都会停止。
At the moment the function looks at the second drop down and sets the options for it and I added the line 目前,该功能在第二个下拉菜单中进行了设置,并为其设置了选项。
document.getElementById('fld_ClientID').value ="" document.getElementById('fld_ClientID')。value =“”
to get it to enter the data in the text box. 使其在文本框中输入数据。 How do I get rid of the reference to the tblPromotions completely and make it get the data for the text box only.
我如何完全摆脱对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>
maybe, you shouldn't do a mysql query inside a javascript function. 也许,您不应该在javascript函数中执行mysql查询。 you should either: use ajax to get the possible options or query all the possible options once then save it on a global variable
您应该:使用ajax获取可能的选项或查询所有可能的选项一次,然后将其保存在全局变量中
something like: 就像是:
<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());">
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.