javascript 将 DropDown 选定的值与其他选定的查询结果集一起传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4752393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:46:47  来源:igfitidea点击:

Passing DropDown selected value along with other selected query result set

javascriptjavascript-eventscoldfusion

提问by vas

I have a form "test.cfm" It passes values to an action page "testAction.cfm"!

我有一个表单“test.cfm”,它将值传递给一个操作页面“testAction.cfm”!

test.cfm does the following:-

test.cfm 执行以下操作:-

  1. A drop down box “fruitsList”.

  2. A query “qryFruits” that pulls data and then displays the query-result set

  3. “Add” button for every query-result set

  1. 一个下拉框“fruitsList”。

  2. 一个查询“qryFruits”,它提取数据然后显示查询结果集

  3. 每个查询结果集的“添加”按钮

How it should work:-

它应该如何工作:-

  1. The dropdown “fruitsList” is to be selected.

  2. Once the “Add” button in the result list is selected the “query-result set -CurrentRow” value is to be passed.

    Add button is passing the “query-resultset -CurrentRow” value but NOT the "Drop down" Value.

  1. 将选择下拉菜单“fruitsList”。

  2. 一旦选择了结果列表中的“添加”按钮,就会传递“查询结果集 -CurrentRow”值。

    添加按钮传递的是“query-resultset -CurrentRow”值,而不是“Drop down”值。

How to pass the “dropdown- selected-value”?

如何传递“下拉选择值”?

    <!--- test.cfm --->
    <script type="text/javascript" >
          function assign_fruits()
         //  
         {
           var a = document.fruitsForm.fruitsList.selectedIndex;
           document.getElementById('salesForce').value = document.fruitsForm.fruitsList.options[a].value; 
           }
        </script>

    <form name="fruitsForm" >
    <table>
      <tr>
       <td>
        <select name="fruitsList" onChange="assign_fruits()">
         <option disabled="true">select One
         <option value="m1">apple
         <option value="m2" selected>orange
         <option value="m3">banana
         <option value="m4">grape
         <option value="m5">mango
        </select> 
       </td>
<!--- Trying to assign the DD-value to an input field --->
 <input  name="salesForce" value=""> 
      </tr> 
     <cfquery name="qryFruits" datasoure="#dsn#">
      Select values from Fruits_Table
     </cfquery>
     <cfloop startrow="1" endrow="#qryFruits.recordcount#" query="qryFruits">
      <cfoutput>
      <tr> 
       <td><a href="testAction.cfm">Add</a></td>     
      </tr>   
      </cfoutput>
     </cfloop>
    </table>

    </form>

Kindly help. Vas

请帮助。瓦斯

回答by JhnSctt

You are going about this a bit unconventionally but I believe the following does what you're looking for:

您对此有点非常规,但我相信以下内容可以满足您的需求:

 <cfdump var="#form#">

<!--- test.cfm --->
<cfset qryFruits = queryNew("fruitID,fruitName")>
<cfset queryAddRow(qryFruits, 2)>
<cfset querySetCell(qryFruits, "fruitID", 1, 1)>
<cfset querySetCell(qryFruits, "fruitName", "Kiwi", 1)>
<cfset querySetCell(qryFruits, "fruitID", 2, 2)>
<cfset querySetCell(qryFruits, "fruitName", "Lime", 2)>

<script type="text/javascript" >
    function assign_fruits() {
        var a = document.fruitsForm.fruitsList.selectedIndex;
        document.getElementById('salesForce').value = document.fruitsForm.fruitsList.options[a].value; 
    }
    function submit(id) {
        document.getElementById('fruitClicked').value = id;
        document.forms["fruitsForm"].submit();
    }
</script>

<form name="fruitsForm" id="fruitsForm" action="index.cfm" method="post">
    <p>
        <select name="fruitsList" onChange="assign_fruits();">
            <option value="">select One</option>
            <option value="m1">apple</option>
            <option value="m2" selected>orange</option>
            <option value="m3">banana</option>
            <option value="m4">grape</option>
            <option value="m5">mango</option>
        </select>
    </p>

    <!--- Trying to assign the DD-value to an input field --->
    <p>Fruit Slected from List Above: <input name="salesForce" id="salesForce" value=""></p>
    <p>Fruit Clicked Below: <input name="fruitClicked" id="fruitClicked" value=""></p>
    <cfloop startrow="1" endrow="#qryFruits.recordcount#" query="qryFruits">
    <cfoutput>
        <div></div><a href="##" onclick="submit(#qryFruits.fruitID#);">#qryFruits.fruitName#</a></div>
    </cfoutput>
    </cfloop>
</form>

Note that you are missing "id" attributes in some of your elements which was causing your JavaScript to fail.

请注意,您在导致 JavaScript 失败的某些元素中缺少“id”属性。

回答by David Pean

I'm not quite sure what the end result is, but are you just trying to pass whatever is output in the #values# to that javascript function?

我不太确定最终结果是什么,但是您是否只是想将 #values# 中的任何输出传递给该 javascript 函数?

You can always pass the value directly.

您始终可以直接传递值。

<tr>
     <td>#values#  </td> 
     <td><a href="test.cfm" onClick="assign_fruits(#values#);">Add</a></td>          
</tr> 

回答by duncan

Your javascript says var a = document.fruitsForm.fruits.selectedIndex;but your form field isn't called 'fruits', it's called fruitsList. Try

您的 javascript 说, var a = document.fruitsForm.fruits.selectedIndex;但您的表单字段不称为“fruits”,而是称为fruitsList。尝试

document.fruitsForm.fruitsList.selectedIndex;

instead.

反而。

回答by duncan

Your output can be simplified slightly. But I'm still a little unclear what you're trying to do. What's the purpose of testAction.cfm?

您的输出可以稍微简化。但我仍然有点不清楚你想要做什么。testAction.cfm 的目的是什么?

<cfoutput query="qryFruits">
        <tr> 
            <td><a href="testAction.cfm?ID=#qryFruits.CurrentRow#">Add #qryFruits.Values#</a></td>           
        </tr>   
</cfoutput>

回答by duncan

OK, I'm still not sure exactly what you're trying to do. I assume you want to pass a parameter, salesForce, to testAction.cfm. And the value of it should be m1, m2, m3, ...

好的,我仍然不确定您到底要做什么。我假设您想将参数 salesForce 传递给 testAction.cfm。它的值应该是 m1, m2, m3, ...

So essentially you need to have your urls be like testAction.cfm?salesForce=m1Is this correct? If so, get rid of the hidden form field and all that javascript. Is it the case that the fruitsList dropdown is always numbered m1 - mX in sequence? (i.e. you don't go m1, m79, m4, m2, m99 etc).

所以基本上你需要让你的网址像testAction.cfm?salesForce=m1这样正确吗?如果是这样,摆脱隐藏的表单字段和所有的 javascript。是不是 FruitList 下拉菜单总是按顺序编号 m1 - mX?(即你不去 m1、m79、m4、m2、m99 等)。

And here's an assumption - does the list of fruits in your dropdown correspond to the fruits query? i.e. is it the same fruits in the same order? If so you should just ditch the dropdown and only have the links (or vice versa).

这是一个假设 - 下拉列表中的水果列表是否与水果查询相对应?即是相同顺序的相同水果吗?如果是这样,您应该放弃下拉菜单而只有链接(反之亦然)。

<!--- test.cfm --->
<cfquery name="qryFruits" datasoure="#dsn#">
  Select values from Fruits_Table
</cfquery>

<table> 
  <cfoutput query="qryFruits">
    <tr> 
        <td><a href="testAction.cfm?salesForce=m#qryFruits.CurrentRow#">Add #qryFruits.Values#</a></td>           
    </tr>   
  </cfoutput>
</table>