简体   繁体   English

使用Javascript将变量连接到表单输入值上

[英]Concatenate Variable Onto Form Input Value Using Javascript

I have a PHP while loop that generates a table containing input tags. 我有一个PHP while循环,该循环生成包含input标签的表。 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. 我正在尝试修改我的javascript / jquery脚本以使用每个生成的输入框名称。

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. 例如,输入框的位置将被命名为position1,position2,position3等。我使用onchange =“ get(row)”调用脚本,其中row是循环实例。 so for position1 the onchange will be onchange"get(1)" 因此,对于position1,onchange将为onchange“ get(1)”

This is passed to my javascript / jquery script: 这被传递给我的javascript / jquery脚本:

<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. 如您所见,我想将变量postvarposition设置为输入框(位置)的id +行号。 I want to concatenate the two together inside the form.position.value statement. 我想在form.position.value语句中将两者串联在一起。 So get(1) will then produce: 因此,get(1)将产生:

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. 下面的代码适用于第一行,在该行中我手动在输入标签名称后附加了“ 1”。

<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. 然后,我尝试按照machineghost的建议进行更改,但这仍然行不通。 I have confirmed that the value of variable row is 1 with the alert message. 我已经通过警报消息确认变量row的值为1。

<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 Google Chrome(F12)输出错误Uncaught ReferenceError: position is not defined

any advice or suggestions would be appreciated. 任何意见或建议,将不胜感激。

Many Thanks 非常感谢

If you want to use the bracket syntax you can't combine it with period syntax. 如果要使用方括号语法,则不能将其与句点语法结合使用。 In other words: 换一种说法:

form.[position+row].value

should be: 应该:

form[position+row].value

Working 100% now. 现在工作100%。

should be 应该

form["position"+row].value as per advice from machineghost. 根据machineghost的建议,形成form [“ position” + row] .value。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM