如何使用AJAX/PHP获取具有相同ID的动态生成文本框的值? [英] How to get value of dynamically generated textbox with same id using AJAX/PHP?

查看:60
本文介绍了如何使用AJAX/PHP获取具有相同ID的动态生成文本框的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此网页中,我正在动态生成多个文本框,每个文本框都具有唯一的值,并且我想动态获取该值.但是我无法根据其位置捕获文本框的值.此代码仅适用于第一个生成的文本框.我有这样的代码

In this webpage I am generating multiple textbox dynamically and each textbox is meant to hold unique value and I want to get that value dynamically.But I'm not being able to catch the value of the textbox according to its position. This code is only working for the firstly generated textbox. I have code like this

<tr>
<td  align="center"><input type="text" name="serialNoArray[]" id="serialArray" onChange="checkusername()" ><span id="std_id_status"></span></td>
</tr>

<script> 
function checkusername() { 
    var s = _("serialArray").value; 
    if(s != "") {  
        _("std_id_status").innerHTML = 'checking ...'; 
        var ajax = ajaxObj("POST", "sellingDetails.php");
        ajax.onreadystatechange = function() { 
            if(ajaxReturn(ajax) == true){
                _("std_id_status").innerHTML = ajax.responseText;
            }
        }
        ajax.send("std_id_check="+s);
      }             
}    
</script>

推荐答案

首先,您应该使用不是id的类,因为id的元素对于整个文档必须是唯一的.而且由于使用onChange,所以可以使用 this 传递元素,就像 onChange ="checkusername(this)" 一样.我想您也应该更改 restrict 函数 onkeyup ="restrict('serialArray')" 的代码,但是我没有看到该代码,所以我无法为您提供更多帮助如果您也不提供此代码...

First you should use classes not id, because an element with id must be unique for the entire document. And since you use onChange you can pass the element using this like that onChange="checkusername(this)" . I guess you should also change the code of the restrict function onkeyup="restrict('serialArray')" also but i do not see that code so I cannot help you more if you do not provide this code too...

    <tr>
    <td  align="center"><input type="text" name="serialNoArray[]" class="serialArray" onkeyup="restrict('serialArray')" onChange="checkusername(this)" ><span class="std_id_status"></span></td>
    </tr>

然后,您只能获取要更改的元素的值,并且仅更改匹配范围的html.(我在示例中使用jQuery,因此应将其包含在文档中.)

Then you can get only the value of the element being changed and change the html of the matching span only.(I use jQuery in the example so you should include it in your document.)

<script>
    function checkusername(s) {

        if (s.value != "") {
            $(s).nextAll('.std_id_status').first().html('checking ...');
            var ajax = ajaxObj("POST", "sellingDetails.php");
            ajax.onreadystatechange = function() {
                if (ajaxReturn(ajax) == true) {
                  $(s).nextAll('.std_id_status').first().html(ajax.responseText);
                }
            }
            ajax.send("std_id_check=" + s.value);
        }
    }
</script>

由于我没有您的所有JavaScript代码,因此无法对其进行测试,但类似的方法应该可以工作.

Since i do not have all your javascript code I could not test it but something like this should work.

这篇关于如何使用AJAX/PHP获取具有相同ID的动态生成文本框的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆