coldfusion - 如何循环在同一页面中从getElementById传递的值?

时间:2013-07-17 03:51:54

标签: coldfusion

我想从列表中循环选定的数字(cfselect)。我尝试了getElementById但只能在同一页面上显示它。我无法将此数字传递给循环。有人能帮我吗?谢谢。

function item()

        var a = document.formName.numList.selectedIndex;
        document.getElementById('i').value = document. family.tBro.options[a].value;

        var n=document. family.tBro.options[a].value;


<!----OTHER INPUT TEXT BOXES --->

<cfform name="family" action="complete.cfm" method="post">

     How many brothers do you have?
     <cfselect name="tBro" onChange="item();" required="yes">
        <option value="1"> 1</option>
        <option value="2"> 2</option> 
        <option value="3"> 3</option>  
        <option value="4">4</option>
    </cfselect>                                                    

   <!---DISPLAY THE SELECTED CHOICE from getElementById--->
   Total number of brothers: <cfinput type="text" name="i" id="i">


   <!---LOOP x amount of time  from selected choice above. 
       For example, if 2 is selected, the below info will display two times
   --->

   <cfinput type="text" name="firstname"  required="yes">
   <cfinput type="text" name="lastname"  required="yes">
   <cfinput type="text" name="Age"  required="yes">
   <cfinput type="text" name="Ocupation"  required="yes">

   <!--- END LOOP--->             

1 个答案:

答案 0 :(得分:5)

提示:陈述您要完成的任务,而不是实施。我不得不重读几次以了解你的需求,在这种情况下你的实现并不是很合适。

您正在尝试将JavaScript传递给CFM代码:这不是它的工作方式。 ColdFusion在服务器上呈现; JavaScript在客户端上呈现。在调用item()时,ColdFusion已完成所有渲染;你不能用item()的结果来影响CF循环。

如果没有真正复杂的AJAX解决方案,您有两种选择:

  1. 在调用init()时刷新浏览器,在url中传递dropdown值(不好,因为你丢失了其他表单字段的状态)
  2. 使用jQuery等其他内容来呈现动态文本字段列表。这可能是最好的方法,也是常见的方法。这样做的缺点是你需要在jQuery中实现“必需”之类的东西,这不是什么大问题,也是一个常见的用例。