gpt4 book ai didi

javascript - 提交表单后保留下拉值

转载 作者:行者123 更新时间:2023-11-28 05:58:30 26 4
gpt4 key购买 nike

动态创建下拉框并通过 JavaScript 数组添加选项,我想在提交表单后保留这些值。假设我选择“OOR”和“2”,然后在提交表单后,我想在这些下拉列表中看到这些值。

谢谢。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>

<script language="javascript">

OORs=new Array("1","2","3","4");
NoOORs=new Array("A","B","C");

populateSelect();
$(function() {


$('#fenv').change(function(){
populateSelect();
});

});
function populateSelect(){

fenv=$('#fenv').val();

$('#market').html('');
if(fenv=='OOR'){
$.each(OORs,function(index,t) {
$("#market").append("<option value='"+t+"'>" +t+ "</option>");
});
}
else {
$.each(NoOORs,function(index,t) {
$("#market").append("<option value='"+t+"'>" +t+ "</option>");
});

}

}
</script>

<form>
<select id="fenv" NAME="fenv">
<option value="OOR2">OOR2</option>
<option value="OOR">OOR</option>

</select>

<select id="market" name="market"></select>
<input type="submit" name="submit" value="submit" >
</form>

最佳答案

您可以利用隐藏字段在表单提交后保留数据。像这样:

    OORs=new Array("1","2","3","4");
NoOORs=new Array("A","B","C");

populateSelect();
$(function() {


$('#fenv').change(function(){
populateSelect();
});

});
function populateSelect(){

fenv=$('#fenv').val();
marketvalues = [];

$('#market').html('');
if(fenv=='OOR'){
$.each(OORs,function(index,t) {
$("#market").append("<option value='"+t+"'>" +t+ "</option>");
marketvalues.push(t);
});
}
else {
$.each(NoOORs,function(index,t) {
$("#market").append("<option value='"+t+"'>" +t+ "</option>");
marketvalues.push(t);
});

}
$("#marketvalues").val(marketvalues.join(","));
}
</script>

<form method="post">
<select id="fenv" NAME="fenv">
<option value="OOR2" <cfif structKeyExists(form, "fenv") and form.fenv EQ "OOR2"> selected="selected"</cfif>>OOR2</option>
<option value="OOR" <cfif structKeyExists(form, "fenv") and form.fenv EQ "OOR"> selected="selected"</cfif>>OOR</option>

</select>

<select id="market" name="market">
<cfif structKeyExists(form, "marketvalues") and trim(form.marketvalues) NEQ "">
<cfloop list="#form.marketvalues#" index="mv">
<option value="#mv#" <cfif form.market EQ mv> selected="selected"</cfif>>#mv#</option>
</cfloop>
</cfif>
</select>
<input type="submit" name="submit" value="submit"/>
<input type="hidden" name="marketvalues" id="marketvalues" value=""/>
</form>

关于javascript - 提交表单后保留下拉值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37467529/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com