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

肖恩·卡罗尔

我有一个网页,使用 4 个搜索框的标题和主页正文中的 2 个搜索框。主页正文中的标题表单和表单是相同的,并且查询相同的搜索页面。

我的问题是如何链接下拉列表,以便当一个更改时其他人跟随。

目前我正在使用 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 > 在标题和其他地方也都更改为相同的值时以某种方式动态?

有任何想法吗?

舒巴姆昆杜

假设您有 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。

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

连接选择.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>

希望它有所帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在wordpress插件中保存选择选项下拉列表

来自分类Dev

如何通过ajax post发送选择选项下拉列表的选项值(而不是里面的html文本)?

来自分类Dev

如何从选择选项下拉列表中获取值并在switch语句中使用该值

来自分类Dev

ng-options未填充选择选项下拉列表

来自分类Dev

如何在 Android 表单中进行过滤的类似选择选项的下拉列表?

来自分类Dev

如何在Angular Directive中使用选择选项下拉菜单?

来自分类Dev

如何使用 Angular 中的反应形式获取选择选项下拉文本值

来自分类Dev

如何重新填充角度引物下拉列表的选择选项

来自分类Dev

在 3 个选择选项下拉列表之间使用 ngModel 绑定数据时遇到问题。(角)

来自分类Dev

HTML / PHP表单选择选项,带有指向MySQL的链接

来自分类Dev

下拉选择选项以过滤Django列表

来自分类Dev

硒选择选项的下拉列表

来自分类Dev

angularjs 中未选择默认选项下拉列表

来自分类Dev

如何在访问表单中默认选择选项组中的单选按钮

来自分类Dev

如何在PHP中编辑和更新表单选择选项值

来自分类Dev

根据表格内容创建选择选项下拉菜单

来自分类Dev

使用JavaScript动态生成的选择选项下拉菜单

来自分类Dev

用于多个选择选项下拉菜单的本地存储

来自分类Dev

使用JavaScript动态生成的选择选项下拉菜单

来自分类Dev

在HTML选择选项下拉菜单中添加输入功能

来自分类Dev

引导程序选择选项下拉箭头未显示

来自分类Dev

重新填充表单中的单选按钮和选择选项

来自分类Dev

Django表单选择选项无法识别,if语句失败

来自分类Dev

动态生成的选择(下拉)列表中的选择选项

来自分类Dev

选择选项时,在选择下拉列表组中禁用选择选项

来自分类Dev

如何在下拉菜单的标题选项下将菜单选项居中?

来自分类Dev

动态选择选项时如何更改下拉列表的类别

来自分类Dev

在下拉列表中获取未选择选项的值

来自分类Dev

angular js下拉列表中的Python硒选择选项

Related 相关文章

  1. 1

    如何在wordpress插件中保存选择选项下拉列表

  2. 2

    如何通过ajax post发送选择选项下拉列表的选项值(而不是里面的html文本)?

  3. 3

    如何从选择选项下拉列表中获取值并在switch语句中使用该值

  4. 4

    ng-options未填充选择选项下拉列表

  5. 5

    如何在 Android 表单中进行过滤的类似选择选项的下拉列表?

  6. 6

    如何在Angular Directive中使用选择选项下拉菜单?

  7. 7

    如何使用 Angular 中的反应形式获取选择选项下拉文本值

  8. 8

    如何重新填充角度引物下拉列表的选择选项

  9. 9

    在 3 个选择选项下拉列表之间使用 ngModel 绑定数据时遇到问题。(角)

  10. 10

    HTML / PHP表单选择选项,带有指向MySQL的链接

  11. 11

    下拉选择选项以过滤Django列表

  12. 12

    硒选择选项的下拉列表

  13. 13

    angularjs 中未选择默认选项下拉列表

  14. 14

    如何在访问表单中默认选择选项组中的单选按钮

  15. 15

    如何在PHP中编辑和更新表单选择选项值

  16. 16

    根据表格内容创建选择选项下拉菜单

  17. 17

    使用JavaScript动态生成的选择选项下拉菜单

  18. 18

    用于多个选择选项下拉菜单的本地存储

  19. 19

    使用JavaScript动态生成的选择选项下拉菜单

  20. 20

    在HTML选择选项下拉菜单中添加输入功能

  21. 21

    引导程序选择选项下拉箭头未显示

  22. 22

    重新填充表单中的单选按钮和选择选项

  23. 23

    Django表单选择选项无法识别,if语句失败

  24. 24

    动态生成的选择(下拉)列表中的选择选项

  25. 25

    选择选项时,在选择下拉列表组中禁用选择选项

  26. 26

    如何在下拉菜单的标题选项下将菜单选项居中?

  27. 27

    动态选择选项时如何更改下拉列表的类别

  28. 28

    在下拉列表中获取未选择选项的值

  29. 29

    angular js下拉列表中的Python硒选择选项

热门标签

归档