1

我一直在寻找这个公共汽车的解决方案,但无法解决。当我想要一个动态下拉菜单来调整第二个下拉菜单中的值并在文本框中填写文本值时,我设法让它工作。

现在我想删掉第二步:即。我实际上想摆脱第二个下拉菜单,只需在文本框中输入一个值。我已经尝试了一切来删除第二步,但是一旦我这样做了,一切都停止了。

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

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> 
4

1 回答 1

0

也许,您不应该在 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());">  

    ...
于 2012-11-12T09:45:58.083 回答