简体   繁体   English

动态更改表单中的选择值,如何保存它们

[英]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文件中,并在所需页面中包含该文件;

array-form.js array-form.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.

相关问题 创建动态表单元素,如何保存它们及其状态? - Creating dynamic form elements, how to save them and their state? 表单中的动态选择值 - Dynamic select values in form 中止AJAX和表单提交,保存它们,更改它们,然后再发送 - Abort AJAX and form submitions, save them, change them, and send it later 在 ReactJS 中保存动态表单中的值 - Save values from dynamic form in ReactJS 如何将多个表单值保存为本地存储中的合并字符串并检索它们以显示在浏览器上 - How to save multiple form values as a consolidated String in Local Storage and retrieve them to be displayed on the browser 如何从动态表单中获取值,将其传递给变量并使用JavaScript中的查询字符串参数重定向到URL? - How to get values from dynamic form, pass them to a variable and redirect to a URL with query string parameters in JavaScript? 如何使用jquery更改连接的值以选择动态形式的更改? - How can I change the value that is connected to select change in dynamic form using jquery? 如何从 sessionStorage 中检索多个选择值并将它们放入原始表单字段中 - How to retrieve multiple select values from sessionStorage and put them into the original form field 当值是动态的时,如何基于第一个选择列表选项更改第二个选择列表? - How to change a second select list based on the first select list option when values are dynamic? 选择代码的形式,如果其中任何一个发生更改,我想触发一个事件 - Form of select tags, I want to fire an event if any of them change
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM