【问题标题】:Dynamic drop down list to fill in a text box动态下拉列表填写文本框
【发布时间】:2012-11-12 07:52:06
【问题描述】:

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

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

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

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> 

【问题讨论】:

    标签: php javascript mysql drop-down-menu onchange


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      相关资源
      最近更新 更多