weixin_43612885
weixin_43612885
采纳率0%
2018-11-05 11:52 阅读 1.4k

如何在js中获取ajax动态生成的元素?

我有一个php1,向php2发送ajax请求加载一个form到div里,如何获取form的输入框的值?
php1:

<script>
 function querengaipan()
    {
        var jilufanhui = document.getElementById("jilufanhui").value;
        var str = document.getElementById("caozuo").value;
        var gaipanid = document.getElementById("gaipanid").value;
        var riqi = document.getElementById("gaipanriqi").value;
        var zhi = document.getElementById("gaipanzhi").value;
        var beizhu = document.getElementById("gaipanbeizhu").value;
        if (str!="gaipan")
        {
            return;
        }
        var getStr = "caozuo=gaipan&gaipanid"+ gaipanid +"&gaipanriqi="+ riqi +"&gaipanzhi="+ zhi +"&gaipanbeizhu="+ beizhu;
        if (window.XMLHttpRequest)
        {
            // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
            xmlhttp=new XMLHttpRequest();
        }
        else
        {
            // IE6, IE5 浏览器执行代码
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("jilufanhui").innerHTML="";
                document.getElementById("jilufanhui").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","jilucontroller.php?"+getStr,true);
        xmlhttp.send();
    }
    </script>
    <div id="jilufanhui"></div>

php2:

 echo '<form onsubmit="return false" action="##">
            <input type="hidden" id="caozuo" name="caozuo" value="gaipan">
            <input type="hidden" name="gaipanid" value="'. $gaipanid .'">
            <div class="form-group">
            <label for="gaipanxingming">姓名/学号:</label>
            <input type="text" class="form-control" id="gaipanxingming" name="gaipanxingming" value="'. $xingming .'" placeholder="在此输入姓名或学号" disabled="disabled">
            </div>
            ……一些其他输入框……
            <center>
            <button type="button" class="btn btn-primary" onclick="querengaipan()">改判/删除</button>
            </center>
            </form>
            <br />';

点击 改判/删除按钮会提示 SCRIPT5007: Unable to get property 'value' of undefined or null reference

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • caozhy 从今以后生命中的每一秒都属于我爱的人 2018-11-05 16:26
        var jilufanhui = document.getElementById("jilufanhui").value;
        var str = document.getElementById("caozuo").value;
        var gaipanid = document.getElementById("gaipanid").value;
        var riqi = document.getElementById("gaipanriqi").value;
        var zhi = document.getElementById("gaipanzhi").value;
        var beizhu = document.getElementById("gaipanbeizhu").value;
    

    这里的某一个没有找到,检查下id是不是写错了,和下面的对照下。

    点赞 评论 复制链接分享

相关推荐