How to pass table-row index to a javascript function
I have some php code that is creating an html table. Within it I have a form where I have different dropdown menus that have onChange events. I need to pass into the javascript function the table-row index. Is there any way to do this? If so could you please provide an example. Thank you!
My php code is listed below:
<?php
echo"
<table id='inputTable'>
<form id='trade' name='trade' method='post' action=''>
<tr id = 'labelRow0' name = 'labelRow0' style='display:table-row'>
<td>Spread Type</td>
<td>Option/Future</td>
<td>Year</td>
<td>Month</td>
<td>Strike</td>
<td>Call/Put</td>
<td>Buy/Sell</td>
<td>Price</td>
<td>Quantity</td>
<td>Delta</td>
<td>House</td>
</tr>
<tr id = 'inputRow0' name = 'inputRow0' style='display:table-row'>
<td>
<select onchange='testChanges(0)' name='spreadType[]' id='spreadType0'>
<option value=''>
<option value='Spread'>Spread
<option value='Fly'>Fly
<option value='Straddle'>Straddle
<option value='Strangle'>Strangle
<option value='Tree'>Tree
<option value='Condor'>Condor
<option value='Ladder'>Ladder
</select>
</td>
<td>
<select name='optionFuture[]' id='optionFuture0'>
<option value=''>
<option value='Option'>Option
<option value='Future'>Future
</select>
<开发者_如何学编程/td>
<td>
<select name='year[]' id='year0'>
<option value=''>
<option value='Front'>Front
<option value='Short'>Short
<option value='Red'>Red
<option value='Green'>Green
<option value='Blue'>Blue
<option value='Gold'>Gold
</select>
</td>
<td>
<select name='month[]' id='month0'>
<option value=''>
<option value='January'>January
<option value='February'>February
<option value='March'>March
<option value='April'>April
<option value='May'>May
<option value='June'>June
<option value='July'>July
<option value='August'>August
<option value='September'>September
<option value='October'>October
<option value='November'>November
<option value='December'>December
</select>
</td>
<td>
<select name='strike[]' id='strike0'>
<option value=''>
<option value='100'>100
<option value='99'>99
<option value='98'>98
<option value='97'>97
<option value='96'>96
</select>
</td>
<td>
<select onchange='testChanges()' name='callPut[]' id='callPut0'>
<option value=''>
<option value='Call'>Call
<option value='Put'>Put
</select>
</td>
<td>
<select name='buySell[]' id='buySell0'>
<option value=''>
<option value='Buy'>Buy
<option value='Sell'>Sell
</select>
</td>
<td>
<input type='text' name='price[]' id='price0' size='9'/>
</td>
<td>
<input type='text' name='quantity[]' id='quantity0' size='9'/>
</td>
<td>
<input type='text' name='delta[]' id='delta0' size='9'/>
</td>
<td>
<select onchange='testChanges()' name='house[]' id='house0'>
<option value=''>
<option value='001 - First Options'>001 - First Options
<option value='020 - Bache'>020 - Bache
<option value='033 - FC Stone'>033 - FC Stone
<option value='Other'>Other
</select>
</td>
</tr>
<tr id = 'otherRow0' name = 'otherRow' style='display:none'>
<td><input type='text' name='spreadFormation[]' id='spreadFormation0' size='9'/></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td><input type='text' name='otherHouse[]' id='otherHouse0' size='22'/></td>
</tr>";
$baseLabel = 'labelRow';
$baseInput = 'inputRow';
$baseStr = 'otherRow';
$baseOptionFuture = 'optionFuture';
$baseYear = 'year';
$baseMonth = 'month';
$baseStrike = 'strike';
$baseCallPut = 'callPut';
$baseBuySell = 'buySell';
$baseSpreadType = 'spreadType';
$basePrice = 'price';
$baseQuantity = 'quantity';
$baseDelta = 'delta';
$baseHouse = 'house';
$baseSpreadFormation = 'spreadFormation';
$baseOtherHouse = 'otherHouse';
for ( $i = 1; $i < 20; $i++ )
{
$labelId = $baseLabel.$i;
$inputRow = $baseInput.$i;
$otherId = $baseStr.$i;
$optionFutureId = $baseOptionFuture.$i;
$yearId = $baseYear.$i;
$monthId = $baseMonth.$i;
$strikeId = $baseStrike.$i;
$callPutId = $baseCallPut.$i;
$buySellId = $baseBuySell.$i;
$spreadTypeId = $baseSpreadType.$i;
$priceId = $basePrice.$i;
$quantityId = $baseQuantity.$i;
$deltaId = $baseDelta.$i;
$houseId = $baseHouse.$i;
$spreadFormationId = $baseSpreadFormation.$i;
$otherHouseId = $baseOtherHouse.$i;
echo"
<tr id = $labelId name = $labelId style='display:none'>
<td>Spread Type</td>
<td>Option/Future</td>
<td>Year</td>
<td>Month</td>
<td>Strike</td>
<td>Call/Put</td>
<td>Buy/Sell</td>
<td>Price</td>
<td>Quantity</td>
<td>Delta</td>
<td>House</td>
</tr>
<tr id = $inputRow name = $inputRow style='display:none'>
<td>
<select onchange='testChanges()' name='spreadType[]' id='spreadType0'>
<option value=''>
<option value='Spread'>Spread
<option value='Fly'>Fly
<option value='Straddle'>Straddle
<option value='Strangle'>Strangle
<option value='Tree'>Tree
<option value='Condor'>Condor
<option value='Ladder'>Ladder
</select>
</td>
<td>
<select name='optionFuture[]' id=$optionFutureId>
<option value=''>
<option value='Option'>Option
<option value='Future'>Future
</select>
</td>
<td>
<select name='year[]' id=$yearId>
<option value=''>
<option value='Front'>Front
<option value='Short'>Short
<option value='Red'>Red
<option value='Green'>Green
<option value='Blue'>Blue
<option value='Gold'>Gold
</select>
</td>
<td>
<select name='month[]' id=$monthId>
<option value=''>
<option value='January'>January
<option value='February'>February
<option value='March'>March
<option value='April'>April
<option value='May'>May
<option value='June'>June
<option value='July'>July
<option value='August'>August
<option value='September'>September
<option value='October'>October
<option value='November'>November
<option value='December'>December
</select>
</td>
<td>
<select name='strike[]' id=$strikeId>
<option value=''>
<option value='100'>100
<option value='99'>99
<option value='98'>98
<option value='97'>97
<option value='96'>96
</select>
</td>
<td>
<select onchange='testChanges()' name='callPut[]' id=$callPutId>
<option value=''>
<option value='Call'>Call
<option value='Put'>Put
</select>
</td>
<td>
<select name='buySell[]' id=$buySellId>
<option value=''>
<option value='Buy'>Buy
<option value='Sell'>Sell
</select>
</td>
<td>
<input type='text' name='price[]' id=$priceId size='9'/>
</td>
<td>
<input type='text' name='quantity[]' id=$quantityId size='9'/>
</td>
<td>
<input type='text' name='delta[]' id=$deltaId size='9'/>
</td>
<td>
<select onchange='testChanges()' name='house[]' id = $houseId>
<option value=''>
<option value='001 - First Options'>001 - First Options
<option value='020 - Bache'>020 - Bache
<option value='033 - FC Stone'>033 - FC Stone
<option value='Other'>Other
</select>
</td>
</tr>
<tr id = $otherId name = $otherId style='display:none'>
<td><input type='text' name='spreadFormation[]' id=$spreadFormationId size='9'/></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td><input type='text' name='otherHouse[]' id=$otherHouseId size='22'/></td>
</tr>
";
}
echo "</table>
<input id='submitButton' type='submit' name='submit' type='submit' value='submit' />
</form>";
?>
Im use to Zend, so this is how I usually do it, isntead of echoing html, just type out the html, and then use <?for:?>
syntax. Like the following.
<table id='inputTable'>
<form id='trade' name='trade' method='post' action=''>
<tr id = 'labelRow0' name = 'labelRow0' style='display:table-row'>
<td>Spread Type</td>
<td>Option/Future</td>
<td>Year</td>
<td>Month</td>
<td>Strike</td>
<td>Call/Put</td>
<td>Buy/Sell</td>
<td>Price</td>
<td>Quantity</td>
<td>Delta</td>
<td>House</td>
</tr>
<tr id = 'inputRow0' name = 'inputRow0' style='display:table-row'>
<td>
<select onchange='testChanges()' name='spreadType[]' id='spreadType0'>
<option value=''>
<option value='Spread'>Spread
<option value='Fly'>Fly
<option value='Straddle'>Straddle
<option value='Strangle'>Strangle
<option value='Tree'>Tree
<option value='Condor'>Condor
<option value='Ladder'>Ladder
</select>
</td>
<td>
<select name='optionFuture[]' id='optionFuture0'>
<option value=''>
<option value='Option'>Option
<option value='Future'>Future
</select>
</td>
<td>
<select name='year[]' id='year0'>
<option value=''>
<option value='Front'>Front
<option value='Short'>Short
<option value='Red'>Red
<option value='Green'>Green
<option value='Blue'>Blue
<option value='Gold'>Gold
</select>
</td>
<td>
<select name='month[]' id='month0'>
<option value=''>
<option value='January'>January
<option value='February'>February
<option value='March'>March
<option value='April'>April
<option value='May'>May
<option value='June'>June
<option value='July'>July
<option value='August'>August
<option value='September'>September
<option value='October'>October
<option value='November'>November
<option value='December'>December
</select>
</td>
<td>
<select name='strike[]' id='strike0'>
<option value=''>
<option value='100'>100
<option value='99'>99
<option value='98'>98
<option value='97'>97
<option value='96'>96
</select>
</td>
<td>
<select onchange='testChanges()' name='callPut[]' id='callPut0'>
<option value=''>
<option value='Call'>Call
<option value='Put'>Put
</select>
</td>
<td>
<select name='buySell[]' id='buySell0'>
<option value=''>
<option value='Buy'>Buy
<option value='Sell'>Sell
</select>
</td>
<td>
<input type='text' name='price[]' id='price0' size='9'/>
</td>
<td>
<input type='text' name='quantity[]' id='quantity0' size='9'/>
</td>
<td>
<input type='text' name='delta[]' id='delta0' size='9'/>
</td>
<td>
<select onchange='testChanges()' name='house[]' id='house0'>
<option value=''>
<option value='001 - First Options'>001 - First Options
<option value='020 - Bache'>020 - Bache
<option value='033 - FC Stone'>033 - FC Stone
<option value='Other'>Other
</select>
</td>
</tr>
<tr id = 'otherRow0' name = 'otherRow' style='display:none'>
<td><input type='text' name='spreadFormation[]' id='spreadFormation0' size='9'/></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td><input type='text' name='otherHouse[]' id='otherHouse0' size='22'/></td>
</tr>";
<?php
$baseLabel = 'labelRow';
$baseInput = 'inputRow';
$baseStr = 'otherRow';
$baseOptionFuture = 'optionFuture';
$baseYear = 'year';
$baseMonth = 'month';
$baseStrike = 'strike';
$baseCallPut = 'callPut';
$baseBuySell = 'buySell';
$baseSpreadType = 'spreadType';
$basePrice = 'price';
$baseQuantity = 'quantity';
$baseDelta = 'delta';
$baseHouse = 'house';
$baseSpreadFormation = 'spreadFormation';
$baseOtherHouse = 'otherHouse';
?>
<?php for ( $i = 1; $i < 20; $i++ ) :?>
<?php
$labelId = $baseLabel.$i;
$inputRow = $baseInput.$i;
$otherId = $baseStr.$i;
$optionFutureId = $baseOptionFuture.$i;
$yearId = $baseYear.$i;
$monthId = $baseMonth.$i;
$strikeId = $baseStrike.$i;
$callPutId = $baseCallPut.$i;
$buySellId = $baseBuySell.$i;
$spreadTypeId = $baseSpreadType.$i;
$priceId = $basePrice.$i;
$quantityId = $baseQuantity.$i;
$deltaId = $baseDelta.$i;
$houseId = $baseHouse.$i;
$spreadFormationId = $baseSpreadFormation.$i;
$otherHouseId = $baseOtherHouse.$i;
?>
<tr id = <?= $labelId ?> name = <?= $labelId ?>style='display:none'>
<td>Spread Type</td>
<td>Option/Future</td>
<td>Year</td>
<td>Month</td>
<td>Strike</td>
<td>Call/Put</td>
<td>Buy/Sell</td>
<td>Price</td>
<td>Quantity</td>
<td>Delta</td>
<td>House</td>
</tr>
<tr id = <?= $inputRow ?> name = <?= $inputRow ?>style='display:none'>
<td>
<select onchange='testChanges()' name='spreadType[]' id='spreadType0'>
<option value=''>
<option value='Spread'>Spread
<option value='Fly'>Fly
<option value='Straddle'>Straddle
<option value='Strangle'>Strangle
<option value='Tree'>Tree
<option value='Condor'>Condor
<option value='Ladder'>Ladder
</select>
</td>
<td>
<select name='optionFuture[]' id=<?= $optionFutureId ?>>
<option value=''>
<option value='Option'>Option
<option value='Future'>Future
</select>
</td>
<td>
<select name='year[]' id=<?= $yearId ?>>
<option value=''>
<option value='Front'>Front
<option value='Short'>Short
<option value='Red'>Red
<option value='Green'>Green
<option value='Blue'>Blue
<option value='Gold'>Gold
</select>
</td>
<td>
<select name='month[]' id=<?= $monthId ?>>
<option value=''>
<option value='January'>January
<option value='February'>February
<option value='March'>March
<option value='April'>April
<option value='May'>May
<option value='June'>June
<option value='July'>July
<option value='August'>August
<option value='September'>September
<option value='October'>October
<option value='November'>November
<option value='December'>December
</select>
</td>
<td>
<select name='strike[]' id=<?= $strikeId ?>>
<option value=''>
<option value='100'>100
<option value='99'>99
<option value='98'>98
<option value='97'>97
<option value='96'>96
</select>
</td>
<td>
<select onchange='testChanges(<?= $i ?>)' name='callPut[]' id= <?= $callPutId ?>>
<option value=''>
<option value='Call'>Call
<option value='Put'>Put
</select>
</td>
<td>
<select name='buySell[]' id=<?= $buySellId ?>>
<option value=''>
<option value='Buy'>Buy
<option value='Sell'>Sell
</select>
</td>
<td>
<input type='text' name='price[]' id=<?= $priceId ?> size='9'/>
</td>
<td>
<input type='text' name='quantity[]' id= <?= $quantityId ?> size='9'/>
</td>
<td>
<input type='text' name='delta[]' id= <?= $deltaId ?> size='9'/>
</td>
<td>
<select onchange='testChanges(<?= $i ?>)' name='house[]' id = <?= $houseId ?>>
<option value=''>
<option value='001 - First Options'>001 - First Options
<option value='020 - Bache'>020 - Bache
<option value='033 - FC Stone'>033 - FC Stone
<option value='Other'>Other
</select>
</td>
</tr>
<tr id = $otherId name = $otherId style='display:none'>
<td><input type='text' name='spreadFormation[]' id= <?= $spreadFormationId ?> size='9'/></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td><input type='text' name='otherHouse[]' id= <?= $otherHouseId ?> size='22'/></td>
</tr>
<? endfor; ?>
</table>
<input id='submitButton' type='submit' name='submit' type='submit' value='submit' />
</form>
Continuation of other answer: Clear up your code by using shorthands.
Then inject the loop number <?= $i ?>
indication row number
Remember <?= is the same as echo
Bunch of different shorthands
IF/ELSE
<? if(true): ?>
<? else: ?>
<? endif; ?>
WHILE
<?php while (expr):?>
statement
...
<?php endwhile;?>
<?php for($i = 5; $i < 20; $i++):?>
<b> Number : <?= $i ?> ?>
<?php endfor; ?>
</body>
精彩评论