简体   繁体   English

如何在使用JavaScript和Coldfusion的情况下填充动态创建的下拉框

[英]How to populate a dynamically created dropdown box in a using javascript and coldfusion

I have a dropdown box in a row that was dynamically created. 我在动态创建的行中有一个下拉框。 I'm populating that box on within the screen. 我正在屏幕上填充该框。 Is there a way that I can use cfquery to get the info from the sql server and populate the dropdown box. 有没有一种方法可以使用cfquery从sql服务器获取信息并填充下拉框。 I would like to do it within the javascript? 我想在javascript中做吗?

Here is my code: 这是我的代码:

<script language="javascript" type="text/javascript">
function addRow() {

    var tbl = document.getElementById('tblSample');
    var lastRow = tbl.rows.length;  
    var iteration = lastRow;
    var row = tbl.insertRow(lastRow);

  // left cell
    var cellLeft = row.insertCell(0);
    var textNode = document.createTextNode(iteration-3);
    cellLeft.appendChild(textNode);

      // select cell
    var cellRightSel = row.insertCell(1);
    var sel = document.createElement('select');
    sel.name = 'sectCode' + iteration;
    sel.id = 'sectCode' + iteration;    
    sel.options[0] = new Option('---Any---', '0');
    sel.options[1] = new Option('Level 0.5: test1, '1');
    sel.options[2] = new Option('Level I: test2', '2');
    sel.options[3] = new Option('Level I.D: test3', '3');
    sel.options[4] = new Option('Level II.1: test4', '4');
    sel.options[5] = new Option('Level II.5: test5', '5');
    cellRightSel.appendChild(sel);

}

Well, if your page is a .cfm (I assumed it was), why not simply generating the whole select HTML using ColdFusion directly? 好吧,如果您的页面是.cfm (我以为是),为什么不直接使用ColdFusion直接生成整个选择的HTML? Is there any special reason you want to avoid this? 您是否有任何特殊原因要避免这种情况?

<select name="test">
    <cfoutput query="yourQuery">
        <option value="#yourQuery.value#">#yourQuery.text#</option>
    </cfoutput>
</select>

However, if you want to pass a data structure from ColdFusion to JavaScript, it can be done using a data interchange format like JSON. 但是,如果要将数据结构从ColdFusion传递到JavaScript,可以使用JSON之类的数据交换格式来完成。 Your JavaScript code could make an Ajax call to retrieve the data, or you could simply output the JSON directly in the page and make it accessible in the JS like this: 您的JavaScript代码可以进行Ajax调用以检索数据,或者您可以直接在页面中直接输出JSON并使其在JS中可访问,如下所示:

<script>
    var optionsData = <cfoutput>#serializeJson(yourQuery)#</cfoutput>;
</script>

In that case, the optionsData JS variable would reference an object that contains your query's data. 在这种情况下, optionsData JS变量将引用一个包含查询数据的对象。 You can find more information about serializing queries here . 您可以在此处找到有关序列化查询的更多信息。

Another way is to use cfform and cfselect : 另一种方法是使用cfformcfselect

<cfform>
    <cfselect name="something" 
              query="yourquery" 
              value="AFieldFromTheQuery" 
              display="AnotherFieldFromTheQuery">

    ... etc
</cfform>

plalx's answer is good, but if you really want to do it in Javascript, you can simply do something like this: plalx的答案很好,但是如果您真的想用Javascript来做,则可以简单地执行以下操作:

sel.options[0] = new Option('---Any---', '0');
<cfoutput query="yourQuery">
    sel.options[#yourQuery.CurrentRow#] = new Option('#yourQuery.value#', '#yourQuery.text#');
</cfoutput>

You may also want to use ColdFusion's JSStringFormat function to prevent things like apostrophes within those values from the query causing any problems in your Javascript: 您可能还想使用ColdFusion的JSStringFormat函数来防止查询中这些值中的撇号之类的问题引起Javascript中的任何问题:

sel.options[0] = new Option('---Any---', '0');
<cfoutput query="yourQuery">
    sel.options[#yourQuery.CurrentRow#] = new Option('#JSStringFormat(yourQuery.value)#', '#JSStringFormat(yourQuery.text)#');
</cfoutput>

You can do as plalx mentioned or you can use the CFSELECT tag similar to this. 您可以按照提到的方法进行操作,也可以使用与此类似的CFSELECT标记。

   <!--- Get data --->
    <CFQUERY DATASOURCE="datasource" NAME="qData">
    SELECT fieldname, ID
    FROM qTable
    ORDER BY ID
    </CFQUERY>

  <cfform>  
    <CFSELECT NAME="name"
    query="qData" 
    display="fieldname"
    width="250" 
    value="ID"><option value="" selected="selected">default value</option></CFSELECT>
  </cfform>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JavaScript从PHP数组中填充带有选项的动态创建的下拉列表 - Populate Dynamically Created Dropdown with Options from PHP array using javascript 如何从文本框中动态填充下拉列表 - How to dynamically populate a dropdown from a text box 如何在jsp中使用javascript动态创建下拉框? - How to create dropdown box dynamically using javascript in jsp? 如何使用javascript / jQuery将值分配给动态创建的输入框 - How to assign value to dynamically created input box using javascript/Jquery 如何使用 promise 动态填充下拉选项 - How to populate dropdown options dynamically using a promise 使用Ajax使用下拉选择填充Coldfusion填充表单 - Coldfusion Populate form with dropdown selection using ajax 使用 javascript 在下拉列表中动态填充数据 api 使用 axois - dynamically populate data in dropdown using javascript api using axois 使用javascript使用与第一个下拉框相同的值填充第二个下拉框? - Populate second dropdown box with same values as the first using javascript? 如何使用JavaScript填充下拉列表? - how to populate dropdown list using using javascript? 如何在 bluebeam 中使用 javascript 创建下拉菜单以自动填充文本框 - How to create a dropdown menu using javascript in bluebeam to auto populate a text box
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM