weixin_33699914 2015-11-09 11:06 采纳率: 0%
浏览 17

Ajax多次添加输入

I've successfully wrote a working Ajax code, but the problem is that i can add an input field only once. I have a submit input, that calls Ajax script, everything works, the input text field appears, but after this if i want to add another text field by clicking on the submit input, it does not work. You only load once (YOLO). How do i write more awesome code that lets me add as many text fields as needed? Thanks for every reply. Here is my code:

index.php:

<head>

<script>
function ajaxobj(){

if (window.XMLHttpRequest){
    xmlhttp = new XMLHttpRequest();
}else {
    xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}

xmlhttp.onreadystatechange = function(){
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200)    {
        document.getElementById('asd').innerHTML = xmlhttp.responseText;
    }   
}

xmlhttp.open('GET', 'ajaxAddInput.php',true);
xmlhttp.send();
}
</script></script>
</head>
<body>
<input type="submit" onclick="ajaxobj();">
<div id="asd"></div>

</body>

the php file:

<?php
echo '<input type=\"text\">';
?>
  • 写回答

1条回答 默认 最新

  • weixin_33705053 2015-11-09 11:14
    关注

    Simply use a standardized library, just like jQuery. With this in mind, you might use the following code:

    $("#asd").on('change', function() {
        var val = $(this).val();
        $.ajax("ajaxAddInput.php", {input: val});
    });
    

    Alternatively, you can use the library with classes as well:

    $('input[type=text]').on('change', function() {
        var val = $(this).val();
        $.ajax("ajaxAddInput.php", {input: val});
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中
  • ¥15 请各位帮我看看是哪里出了问题