javascript - 如何链接到表单选择选项下拉列表

标签 javascript php html web

我有一个网页,其中 2 个在标题中,2 个在主页正文中使用 4 个搜索框。 标题形式和首页正文形式相同,查询相同的搜索页面。

我的问题是如何链接下拉菜单,以便当一个下拉菜单发生更改时,其他下拉菜单也会跟随。

目前,我正在使用 php 在到达页面时切换到适当的类别。
例如: <option value='-1' <?php if ($cat == "-1") {print("selected");}?>>All Categories</option> <option value='0'<?php if ($cat == "0") {print("selected");}?>>Category One</option> <option value='1' <?php if ($cat == "1") {print("selected");}?>>Category Two</option>

在查询后到达页面时效果很好。

但是,因为我的主页上有 4 个表单,所以当用户更改页面上的 < select >< options > 之一时,我希望以某种方式动态地更改标题和其他位置中的其他 < select >< options > 全部更改为也有同样的值(value)吗?

有什么想法吗?

最佳答案

假设您有 2 个“选择”html 元素,如下所示:

测试.html

<html>
    <head>
        <script src="connectSelect.js"></script>
    </head>
    <body onload="connectSelect('first', 'second')">
        <select id="first">
            <option value="0">0</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
        </select>
        <select id="second">
            <option value="0">0</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
        </select>
    </body>
</html>

只需在主体负载上使用已创建的“connectSelect”函数,传递要连接的“Select”元素的 ID。

在 header 中包含 connectSelect.js 文件。下面是代码:

connectSelect.js

function connectSelect(id1, id2) {
    var select1 = document.getElementById(id1);
    var select2 = document.getElementById(id2);
    var i, val1, text1, val2, text2, errText;
    
    //to check whether both the select elements are of same length or not
    if (select1.length != select2.length) {
        alert("connectSelect Function Error: Both the 'Select' elements should have same number of options!");
        return;
    }
    
    //after assuring both the select elements to be of same length
    //to check whether both the select elements have same value and text
    for (i = 0; i < select1.length; i++) {
        val1 = select1.options[i].value;
        text1 = select1.options[i].innerHTML;
        val2 = select2.options[i].value;
        text2 = select2.options[i].innerHTML;
        
        if (val1 != val2 || text1 != text2) {
            errText = "Both the 'Select' elements should have same options with same value and text!";
            errText += "\n";
            errText += "\n";
            errText += "First mismatch: Option " + (i+1);
            alert("connectSelect Function Error: " + errText);
            return;
        }
    }
    
    //after assuring both the select elements to be same
    select1.addEventListener("change", function(){
        var index = this.selectedIndex;
        select2.options[index].selected = true;
    });
    select2.addEventListener("change", function(){
        var index = this.selectedIndex;
        select1.options[index].selected = true;
    });
}

我也插入了代码片段。尝试一下。

function connectSelect(id1, id2) {
    var select1 = document.getElementById(id1);
    var select2 = document.getElementById(id2);
    var i, val1, text1, val2, text2, errText;
    
    //to check whether both the select elements are of same length or not
    if (select1.length != select2.length) {
        alert("connectSelect Function Error: Both the 'Select' elements should have same number of options!");
        return;
    }
    
    //after assuring both the select elements to be of same length
    //to check whether both the select elements have same value and text
    for (i = 0; i < select1.length; i++) {
        val1 = select1.options[i].value;
        text1 = select1.options[i].innerHTML;
        val2 = select2.options[i].value;
        text2 = select2.options[i].innerHTML;
        
        if (val1 != val2 || text1 != text2) {
            errText = "Both the 'Select' elements should have same options with same value and text!";
            errText += "\n";
            errText += "\n";
            errText += "First mismatch: Option " + (i+1);
            alert("connectSelect Function Error: " + errText);
            return;
        }
    }
    
    //after assuring both the select elements to be same
    select1.addEventListener("change", function(){
        var index = this.selectedIndex;
        select2.options[index].selected = true;
    });
    select2.addEventListener("change", function(){
        var index = this.selectedIndex;
        select1.options[index].selected = true;
    });
}
<html>
    <head>
        <script src="test.js"></script>
    </head>
    <body onload="connectSelect('first', 'second')">
        <select id="first">
            <option value="0">0</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
        </select>
        <select id="second">
            <option value="0">0</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
        </select>
    </body>
</html>

希望有帮助。

编辑:为了更好的解决方案,请检查 this (来源:来自该问题的评论)

关于javascript - 如何链接到表单选择选项下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43632008/

相关文章:

javascript - 当点击相应的链接时,如何使用 jQuery 将标签更改为文本字段?

Javascript硬编码对象作为参数,但不引用全局对象

php - Laravel Eloquent : n+1 accessing related model via accessor

PHP为mysql提取日期

PHP Round() 无法正常工作

javascript - 如何使 div 出现在用户屏幕的右下角?

对现有对象调用 new Object() 的 Javascript 优化

javascript - redux-form 6.0 rc.3 版本中添加上传多张图片的字段

jquery - 如何让李父被李子推倒(手机导航)

javascript - 如何更改 <i class"material-icons> 的内容,因为它是 <p>