[英]Dynamic change of select values in a form, how to save them
Html Code HTML代码
<select id = "country">
<option selected = "selected" value = "0">all</option>
<option value = "1">country1</option>
<option value = "2">country2</option>
<option value = "3">country3</option>
</select>
<select id = "region">
<option selected = "selected">all</option>
</select>
Javascript Code JavaScript代码
//add an option and it's value with jquery append and javascript split.
//Array elements are in form of option|value
//so that we can process them with split
var regions = new Array()
regions[0] = ["all|1"];
regions[1] = ["region1|2","region2|3"]
regions[2] = ["region3|4","region4|5"]
regions[3] = ["region5|6","region6|7"]
$(document).ready(function() {
$("#country").change(function(){
$("#region").empty()
var v = $("#country").val()
for(i=0; i<regions[v].length; i++)
$("#region").append(new Option(regions[v][i].split("|")[0]),
regions[v][i].split("|")[1])
});
});
I will have many different forms like this (so i will have to create many arrays) and every form will be reused to more than one pages, so if i want to add a new option inside a select tag, code must be in one place, so that i will do the changes once. 我将有许多这样的不同形式(因此我将不得不创建许多数组),并且每种形式都将被重复使用到多个页面上,因此,如果我想在select标记内添加新选项,则代码必须放在一个位置,以便我一次进行更改。 The arrays will be really long.. I'm thinking of putting the first select in an external file and retrieve it with php, and the arrays for the second select will be all in an external javascript file that will be included in all my files.. Can you think of a cleaner implementation so that i don't have to make large arrays in a basic javascript file or do you think it's a correct method?
该数组将非常长。.我正在考虑将第一个选择放入一个外部文件中并用php检索它,而第二个选择的数组将全部在一个外部javascript文件中,该文件将包含在我的所有文件中..您能想到一种更清洁的实现方式,这样我就不必在基本的javascript文件中创建大型数组,还是您认为这是一种正确的方法?
尽管您可能需要考虑JSON ,但是使用JavaScript数组没有任何问题。
You can put your array in separate js file and include that file for desired pages; 您可以将数组放在单独的js文件中,并在所需页面中包含该文件;
var regions = new Array()
regions[0] = ["all|1"];
regions[1] = ["region1|2","region2|3"]
regions[2] = ["region3|4","region4|5"]
regions[3] = ["region5|6","region6|7"]
Include this file any page you want 在任何页面上包含此文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.