0

I have a PHP while loop that generates a table containing input tags. Each input box is given a unique name by appending the input box name and the loop value.

<?php
$i=1;
$r=0;
while($i<=$positionjobrows)
{
    ?>
    <tr>
        <td>
            <?PHP echo $i; ?>
        </td>
    <td>
    <input type=text NAME="position<?PHP echo $i; ?>" id="position<?PHP echo $i; ?>"  style="border: 1px solid #2608c3;color:red; width=200px" value="<? echo mysql_result($resultpositionjob,$r,0);?>"> 
    </td>
    <td>
    <input type=text NAME="job<?PHP echo $i; ?>" id="job<?PHP echo $i; ?>"  style="border: 1px solid #2608c3;color:red; width=200px" value="<? echo mysql_result($resultpositionjob,$r,1);?>"> 
    </td>
    <td>
    <SELECT NAME="person<?PHP echo $i; ?>" id="person<?PHP echo $i; ?>" style="border: 1px solid #2608c3;color:red; width=200px" onchange="get(<? echo $i; ?>);"> 
    <OPTION VALUE=0 >
    <?=$optionpeople?> 
    </SELECT>
    </td>
    <td>
    <div id="training<?PHP echo $i; ?>"><font color=grey size=2>Training details will be shown here</div>
    </td>
    </tr>
    <?PHP
    $i++;
    $r++;
    }
    ?>

I am trying to modify my javascript / jquery script to use each of the generated input box names.

For example the input box position will be named position1, position2, position3 etc. I call the script using onchange="get(row)" where row is the loop instance. so for position1 the onchange will be onchange"get(1)"

This is passed to my javascript / jquery script:

<script type="text/javascript">
        function get(row){

        $.post ('getpeopleinjobs.php',{ 
        postvarposition: form.[position+row].value,
        postvarjob: form.job+row.value, 
        postvarperson: form.person+row.value, 
        postrow: row}, 
            function(output){
                $('#training'+row).html(output).show();
                    });
        alert(postvarposition); 

        }

</script>

As you can see, I want to set the variable postvarposition to the id of the inputbox (position) + the row number. I want to concatenate the two together inside the form.position.value statement. So get(1) will then produce:

postvarposition: form.position1.value

My thinking is along these lines, but is not working:

postvarposition: form.[position+row].value,

How can I concatenate the name of the inputbox against the row?

Thanks as always for the help.

UPDATE

This code below works for the first row where I manually append '1' to the input tag name.

<script type="text/javascript">
        function get(row){
        alert(row);
        $.post ('getpeopleinjobs.php',{ 
        postvarposition: form.position1.value,
        postvarjob: form.job1.value, 
        postvarperson: form.person1.value, 
        postrow: row}, 
            function(output){
                $('#training'+row).html(output).show();
                    });
                }
</script>

I have then tried to change it as advised by machineghost but this still doesnt work. I have confirmed that the value of variable row is 1 with the alert message.

<script type="text/javascript">
        function get(row){
        alert(row);
        $.post ('getpeopleinjobs.php',{ 
        postvarposition: form[position+row].value,
        postvarjob: form[job+row].value, 
        postvarperson: form[person+row].value, 
        postrow: row}, 
            function(output){
                $('#training'+row).html(output).show();
                    });
                }
</script>

Google Chrome (F12) ouputs error Uncaught ReferenceError: position is not defined

any advice or suggestions would be appreciated.

Many Thanks

4

2 回答 2

1

如果要使用括号语法,则不能将其与句点语法结合使用。换句话说:

form.[position+row].value

应该:

form[position+row].value
于 2012-07-11T22:34:15.620 回答
0

现在工作100%。

应该

form["position"+row].value 根据 machineghost 的建议。

于 2012-07-12T06:55:46.717 回答