我正在用美国的城市动态地填充一个选择标签。
根据用户选择的状态,城市选择标记将动态填充来自该状态的城市。城市的选项是通过js函数创建的,js函数很好地完成了它的工作。在状态选择html标记内的'onchange‘事件上调用此函数。
与目前的工作方式一样,这些字段的全部都在表单中。每个字段都必须是数据持久化的(您在这些字段中键入的数据必须在表单提交后“填写”)。除动态填充的城市字段外,页面上当前的所有字段都是持久的,并按预期工作。这是通过创建如下格式的CF变量来实现的:
<cfparam name="form.highschoolstate" default="" />
<cfparam name="form.highschoolcity" default="" />
<cfparam name="form.highschool" default="" />
在每个输入中,一个类似于以下内容的格式:
<select name="highschoolstate" id="highschoolstate" required="required" onchange="stateswitch('highschoolstate')" value="#form.highschoolstate#">
然而,有一个扭曲的形式,填充我的“高中城市”字段的城市不是数据持久的。对于每个州,我都有一份所有城市的清单,格式如下:
<option value=\"akiachak\">Akiachak</option>
但是,当(请查看下面的图片以获得结果)时,我尝试使用innerHTML (通过替换select标记的内容)使数据持久,我得到了这段不受欢迎的代码。
<option value=\"akiachak\" <cfif form.highschoolcity EQ \"akiachak\">selected=\"selected\"</cfif>>Akiachak</option>
是否有一个选项可以将这个条件CF语句放入我动态生成的html中,这样我就可以在整个表单中拥有持久的数据了吗?
动态更改select标记的函数:
//Dynamically changes the drop down list when selecting a city/state pair
function stateswitch(id)
{
var myId = id; //ID of the html element we are changing
var stateFlag = false; //This flag turns true when we have selected a state
var highschoolStateFlag = false; //This flag turns true when we have selected a highschool state
var indexInSelect; //Index selected in the select tag
var selectTag1; //Select tag # 1
var selectTag2; //Select tag # 2 that becomes available after select tag # 1 is selected
if(myId == "state")
{
indexInSelect = document.getElementById("state").selectedIndex;
selectTag1 = document.getElementById("state").options;
selectTag2 = document.getElementById("city");
state = selectTag1[indexInSelect].value;
if(selectTag1[0] == "") //If we haven't selected an option before
{
document.getElementById("state").remove(0); //remove the default/null case
stateFlag = true;
}
if(stateFlag)
indexInSelect = indexInSelect - 1; //accounts for offset of default case in indecees to select from
}
else
{
indexInSelect = document.getElementById("highschoolstate").selectedIndex;
selectTag1 = document.getElementById("highschoolstate").options;
selectTag2 = document.getElementById("highschoolcity");
document.getElementById("highschool").disabled = false;
document.getElementById("highschool").placeholder = "Required";
highschoolstate = selectTag1[indexInSelect].value;
if(selectTag1[0] == "") //If we haven't selected an option before
{
document.getElementById("highschoolstate").remove(0); //remove the default/null case
highschoolStateFlag = true;
}
if(highschoolStateFlag)
indexInSelect = indexInSelect - 1; //accounts for offset of default case in indecees to select from
}
selectTag2.disabled = false; //Disable the second select box (because we know at this point we have selected an option for the first one)
switch(selectTag1[indexInSelect].value)
{
case "alabama":
selectTag2.innerHTML="<option value=\"abbeville\" <cfif form.highschoolcity EQ \"abbeville\">selected=\"selected\"</cfif>>Abbeville</option><option value=\"abernant\" <cfif form.highschoolcity EQ \"abernant\">selected=\"selected\"</cfif>>Abernant</option>";
break;
case "ANOTHER_STATE":
selectTag2.innerHTML="etc...<option value=\"...</option>"
break;
//..
}
}
编辑解决方案:我试图做的是不可能的,所以我决定了另一种方法。
发布于 2013-07-08 12:33:08
从您提供的信息来看,我认为问题在于ColdFusion代码中的ColdFusion字符。您需要它来转义JavaScript代码的引号,而不是ColdFusion代码的引号。尝试从<cfif>
代码中的JavaScript语句中删除这些字符。
而不是这样:
<cfif form.highschoolcity EQ \"abbeville\">selected=\"selected\"</cfif>
试试这个:
<cfif form.highschoolcity EQ "abbeville">selected=\"selected\"</cfif>
您不需要转义ColdFusion代码中的引号,因为ColdFusion服务器将在将代码输出到用户浏览器之前处理该代码。
https://stackoverflow.com/questions/17534643
复制相似问题