简体   繁体   English

Javascript/PHP:如何将 select 中的多个值(以逗号分隔)添加到文本字段

[英]Javascript/PHP: How to add multiple values from a select, separated by a comma, to an text field

Id like to add more than one value in text box when the user selects a value from a select field.当用户从 select 字段中选择一个值时,我想在文本框中添加多个值。 If the text field is not empty, the values will be separated by a comma.如果文本字段不为空,则值将用逗号分隔。 Im using php and javascript to populate both fields.我使用 php 和 javascript 来填充这两个字段。 For now, when I try to select another item, the value in the text field is replaced by the nez one.现在,当我尝试 select 另一个项目时,文本字段中的值被 nez 替换。

Here is my code:这是我的代码:

echo' <div>';
                echo' <label for="pnationalite"><strong>Visited countries</strong></label> '; 

                echo' <select onclick="this.form.paysvisites.value=this.options[this.selectedIndex].text;" name="nationalite_ID" > '; 

                $sql = $bdd->query("SELECT * FROM cov_pays"); 

                //Feed the select from the db

                while ($row = $sql->fetch()) 
                {                       
                    echo '<option value="' . $row['LibPays'] . '">' . $row['LibPays'] . '</option>';                            
                }

                echo' </select> ';

                echo' <div>
                        <br /><input type="text" name="paysvisites" placeholder="Please select one country" >
                    </div>

                    ';              

            echo' </div><br/>';

You can use a javascript function to check whether there is already any selected countries in the textbox or not.您可以使用 javascript function 检查文本框中是否已经有任何选定的国家/地区。 You can call the function on onchange event of the select box.您可以在 select 框的 onchange 事件上调用 function。 I have put comments along with the code.我已将注释与代码一起添加。 Hope this helps.希望这可以帮助。

        <select onchange="addCountry(this)" name="nationalite_ID" >     
        <script>
            function addCountry(s) {
                console.log(s.options[s.selectedIndex].text);                    
                var thisCountry = s.options[s.selectedIndex].text; // latest selection                    
                var selected = s.form.paysvisites.value;  // already selected in the textbox
                console.log(selected);                    
                var toAdd = "";                    
                if(selected.length) {
                     toAdd = ", "; // if there are already selected countries add a comma
                }                    
                if(selected.indexOf(thisCountry) === -1) {    // if this selected country is not already in the textbox, add it                    
                    s.form.paysvisites.value+= toAdd + thisCountry;    
                }                    
            }

        </script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用PHP和JavaScript以逗号分隔获取多个值 - Get Multiple Values with comma separated Using PHP and JavaScript 将多重选择作为逗号从选择框分隔为文本输入 - get the multiple selection as comma separated from select box into a text input 如何将多个电子邮件ID添加到文本字段(如Facebook隐私设置),并将电子邮件ID列表转换为逗号分隔的列表? - How to add multiple email ids into a text field (like facebook privacy setting) and convert the email id list into comma separated list? 如何从URL中获取多个逗号分隔值 - How do I get multiple comma separated values from URL 如何从下拉列表中选择多个值,并使用JavaScript或jQuery将其添加到另一个字段 - How to select multiple values from dropdown and add it to another field using JavaScript or jQuery 如何在JavaScript中过滤逗号分隔的整数值 - How to filter comma separated integer values in javascript 如何在JavaScript中计算逗号分隔的值 - How to count comma separated values in javascript 如何在用逗号分隔的文本框中获取多个值 - How to Fetch Multiple Values in Textbox separated by comma 如何在表格的文本字段中获取逗号分隔的值作为在Java脚本中执行数学功能的值? - How to get comma separated values in a text field of a form as values to perform math functions in java script? 如何从数组中返回以逗号分隔的值? - How to return values separated by comma from array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM