提交后,Html选择选项丢失数据 [英] Html select option lost data after submit
问题描述
我有两个select选项和一些数据:
i have two select option with some data:
<select id="S1" name="S1">
<option value="id1">data1</option>
<option value="id2">data2</option>
<option value="id3">data3</option>
</select>
<select id="S2" name="S2">
// data for selected value in S1
</select>
当我选择一个选项时,我使用数据进行提交以填充第二个选择选项,但当我这样做提交时,我失去了第一个选择选项的选定值。我该如何保留第一个选择的选定值?
When i choose one option, i make a submit with the data to fill a second select option, but when i do this submit i lost the selected value of the first select option. How can i keep the selected value for the first select?
更新:我使用mod_plsql,即在plsql过程中的html languaje。
Update: i use mod_plsql, that is html languaje in a plsql procedure.
使用游标获取数据:
With a cursor i get the data:
CURSOR c_departamento IS
SELECT *
FROM sib_s_lugares_geograficos
WHERE tipo ='DE';
然后我用循环填充选择的一个:
And after i fill the select one with a loop:
htp.p('
<select name="opt_departamento" value="'||OPT_DEPARTAMENTO||'" style="width: 135px" >');
FOR regDep IN c_departamento LOOP
htp.p(
'<option selected value="' || regDep.codigo || '">' || regDep.descripcion || '</option>');
END LOOP;
END IF;
</select>');
我这样做是因为我有另一个光标来获取第二个选择的数据,当我做的提交,mod_plsql再次调用该过程,并绘制HTML,当它发生我得到正确的值为第二选择,但我失去了第一选择的值。
I do the submit because i have another Cursor to get the data for the second select one, when i do the submit, mod_plsql call again the procedure and it paint the html, when it happen I get the correct value for the second select but i lost the value selected for the first.
推荐答案
我不知道这是否是最好的方式,但它的工作原理。
$ b
I don't know if this is the best way but it works.
- <下载Jquery的插件:
Download the plugin for Jquery:
在放置onclick事件的元素中,我使用了一个input元素:
In the element put a onclick event, i use a input element:
输入名称=btnacciononclick =setCookieDepartamento(); type =submitvalue =ciudad
input name="btnaccion" onclick="setCookieDepartamento();" type="submit" value="ciudad"
它调用这个函数,将select中的值保存在cookie中:
It call this function, saving the value of the select in a cookie:
函数setCookieDepartamento(){
var select = document.getElementById(opt_departamento);
var valor = select.options [select.selectedIndex] .value;
$ .cookie(departamento,valor);
}
function setCookieDepartamento(){
var select = document.getElementById("opt_departamento");
var valor = select.options[select.selectedIndex].value; $.cookie("departamento", valor); }
在提交后调用此函数,获取保存的值并将其设置为select选项: p>
Call this function after submit, getting the value saved and setting in the select option:
function getCookieDepartamento(){
var departamento = $ .cookie(departamento);
var element = document.getElementById(opt_departamento);
element.value = departamento;
$ .cookie(departamento,null);
}
function getCookieDepartamento(){ var departamento = $.cookie("departamento"); var element = document.getElementById("opt_departamento"); element.value = departamento; $.cookie("departamento", null); }
这就是全部!
That's All!
提交之前:
Before submit:
提交后:
这篇关于提交后,Html选择选项丢失数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!