简体   繁体   English

从第二个输入中删除 datalist 选项

[英]Removing datalist option from second input

I have two HTML datalist inputs (for first and second language) that can't be the same .我有两个不能相同的HTML 数据列表输入(用于第一和第二语言)。 Instead of not accepting the form, I want the first option chosen to be removed dynamically from the second datalist but I can't make anything work with JQuery.我不想接受表格,而是希望从第二个数据列表中动态删除选择的第一个选项,但我无法使用 JQuery 进行任何操作。 Any suggestions with React are also welcome.也欢迎任何有关 React 的建议。

Many thanks!非常感谢!

 <form autocomplete="on" method="POST"> <input id="fLang" type="text" list="language" onchange="removeLang()" placeholder="First language"> <input id="sLang" type="text" list="language" onchange="removeLang()" placeholder="Second language"> <datalist id="language"> <option value="Chinese">China</option> <option value="English">United Kingdom</option> <option value="Russian">Russia</option> </datalist> </form>

You can achieve this using jQuery by using two datalist elements and detecting the change in the inputs as follows:您可以使用jQuery通过使用两个datalist列表元素并检测输入中的更改来实现此目的,如下所示:

<html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>

<body>
    <form autocomplete="on" method="POST">
        <input id="fLang" type="text" list="flanguage" placeholder="First language">
        <input id="sLang" type="text" list="slanguage" placeholder="Second language">
        <datalist id="flanguage">
            <option id="fChinese" value="Chinese">China</option>
            <option id="fEnglish" value="English">United Kingdom</option>
            <option id="fRussian" value="Russian">Russia</option>
        </datalist>
        <datalist id="slanguage">
            <option id="sChinese" value="Chinese">China</option>
            <option id="sEnglish" value="English">United Kingdom</option>
            <option id="sRussian" value="Russian">Russia</option>
        </datalist>
    </form>
</body>
<script>
    var fRemovedItem;
    var sRemovedItem;
    $(document).ready(function () {
        $('#fLang').on('change', function () {
            let first = $('#fLang').val();
            if (first != '') {
                sRemovedItem = $(`#sLanguage option[value='${first}']`);
                sRemovedItem.remove();
            } else {
                let sDatalist = $("#slanguage");
                console.log(sDatalist);
                console.log(sRemovedItem);
                console.log(sDatalist.append(sRemovedItem));
            }
        });
        $('#sLang').on('change', function () {
            let second = $('#sLang').val();
            if (second != '') {
                fRemovedItem = $(`#fLanguage option[value='${second}']`);
                fRemovedItem.remove();
            } else {
                let fDatalist = $("#flanguage");
                console.log(fDatalist.append(fRemovedItem));
            }
        });
    });
</script>

</html>

UPDATE: Removed items never go back if users remove the text in fLang or sLang .更新:如果用户删除fLangsLang This situation was fixed with new code.使用新代码解决了这种情况。

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

相关问题 从数据列表中选择一个选项,并将索引号与第二个数据列表中的另一个选项进行比较 - Select an option from a datalist and compare index number to another option in a second datalist 从数据列表中选取的查看选项 - View option picked from datalist 具有datalist选项的Angular Input,如何区分用户输入并从边缘浏览器中的列表中选择选项 - Angular Input with datalist options, how to differentiate user input and select option from list in edge browser 如何将输入映射到我的数据列表选项? - How to map input to my datalist option? 如何在输入下从数据列表中选择一个选项后立即触发功能? - how to trigger function right after select a option from datalist under a input? 有没有办法使用纯 JS 使用数据列表中的顶部选项自动完成输入的值? - Is there a way to autocomplete an input's value with the top placed option from the datalist using pure JS? 从Javascript的数据列表中获取选定的选项 - Get the selected option from datalist in Javascript HTML datalist 从列表中隐藏选项 - HTML datalist hide option from list 单击时清除输入值并重置数据列表选项下拉列表 - Clear a input value and also reset the datalist option dropdown when clicked 数据列表选项选择上的Internet Explorer 10“输入”事件未触发 - Internet explorer 10 “input” event on datalist option selection not firing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM