简体   繁体   English

从动态添加的文本输入中获取价值

[英]get value from dynamic added text input

i have to add some text input dynamically on my page as following: 我必须在页面上动态添加一些文本输入,如下所示:

$table=""; $n=0;
$arrayform=array();
foreach($rslt as $rows){                
            $table=$table."
                <div class='img-rounded row' style='margin-bottom:2%' >
                    <div class='col-lg-1'></div>
                    <div class='col-lg-4'>
                        <input id='cap-".$n."' type='text' class='form-control' value='".$rows['caption']."'>
                    </div>
                    <div class='col-lg-1' id='rightarrow'>
                        <img id='img-".$n."' src='img/right-arrow.png' width='45' height='30' alt=''/>
                    </div>
                    <div class='col-lg-4'>
                        <input id='val-".$n."' type='text' class='form-control' value='".$rows['value']."'>
                    </div>
                    <div class='col-lg-1'>
                        <input type='button' id='btn-".$n."' onClick='removeval(\"".$rows['caption']."\",\"".$rows['value']."\",\"".$_GET['id']."\",\"".$rows['id']."\")' class='btn btn-danger' value='Remove Column'>
                    </div>
                </div>
                <div id='divrslt-".$n."' class='row text-success' style='margin-left:10%; margin-bottom:2%'>
                </div>              
            ";

the id of form element's determine by $n variable and other strings. 表单元素的ID由$ n变量和其他字符串确定。

i wrote following code to construct parameter list of input text values for send to a javascript function. 我编写了以下代码,以构造要发送到javascript函数的输入文本值的参数列表。

        $arrayform[$rows['caption']]="cap-".$n.".value,val-".$n.".value, ";         $n++;
}

And with following code i use the array that constructed in prior line as parameter to updatechartval() javascript function. 并使用以下代码,我将在前一行中构造的数组用作updatechartval()javascript函数的参数。

$table=$table."
    <div class='row' style='margin-left:4%'>
        <button type='button' class='btn btn-success' onClick='updatechartval(".implode(" ",$arrayform)."\"".$_GET['id']."\")' value='update'>
            Update
        </button>

    </div>
";

And then i echo that: 然后我回应:

echo $table;

the rendered button code is: 呈现的按钮代码为:

<button type="button" class="btn btn-success" onclick="updatechartval(cap-0.value,val-0.value,  cap-1.value,val-1.value,  cap-2.value,val-2.value,  cap-3.value,val-3.value, &quot;126&quot;)" value="update">
            Update

but when i run the code, the id of element's was sent instead value of the text input's. 但是当我运行代码时,发送了元素的id而不是文本输入的值。 how can i solve this? 我该如何解决? thanks... 谢谢...

You are suffering from the javascript equivalent of an SQL injection attack: 您正遭受与SQL注入攻击等效的javascript:

[..snip..] onclick="updatechartval(cap-0.value,val-0.value,[..snip..]
                                   ^^^^^^^^^^^

You need to quote your parameters in the function call, because right now that code is being interpreted/executed as: 您需要在函数调用中引用您的参数,因为现在该代码被解释/执行为:

updateCharval(unknown/undefined variable "cap" minus 0.value, ...

An integer has no .value attribute, and since you're trying to subtract that undefined value attribute from an undefined variable, you're going to get a whole back of stuff blowing up in your face. 整数不具有.value属性,并且由于您尝试从未定义的变量中减去该未定义的值属性,因此您将得到一整堆东西。

Your generator function should look more like: 您的生成器函数应类似于:

$arrayform[$rows['caption']]="'cap-".$n.".value','val-".$n.".value' etc...
                              ^----------------^ ^----------------^

Note the extra quotes. 请注意额外的引号。

You need to get the value of the input from the updatechartval(param1, param2) function. 您需要从updatechartval(param1, param2)函数获取输入的值。 Right now you're trying to run javascript outside of a javascript tag. 现在,您正在尝试在javascript标签之外运行javascript。

So you'd end up with something like this: 因此,您最终会得到如下结果:

function updatechartval(a,b) {
    valueA = document.getElementById(a).value;
    valueB = document.getElementById(b).value;
}

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

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