I have a form "test.cfm" It passes values to an action page "testAction.cfm"!
test.cfm does the following:-
A drop down box “fruitsList”.
A query “qryFruits” that pulls data and then displays the query-result set
“Add” button for every query-result set
How it should work:-
The dropdown “fruitsList” is to be selected.
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.
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>
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.
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?
You can always pass the value directly.
<tr>
<td>#values# </td>
<td><a href="test.cfm" onClick="assign_fruits(#values#);">Add</a></td>
</tr>
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?
<cfoutput query="qryFruits">
<tr>
<td><a href="testAction.cfm?ID=#qryFruits.CurrentRow#">Add #qryFruits.Values#</a></td>
</tr>
</cfoutput>
Your javascript says
var a = document.fruitsForm.fruits.selectedIndex;
but your form field isn't called 'fruits', it's called fruitsList. Try
document.fruitsForm.fruitsList.selectedIndex;
instead.
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, ...
So essentially you need to have your urls be like testAction.cfm?salesForce=m1
Is 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).
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>
精彩评论