Php Ajax多次添加输入

Php Ajax多次添加输入,php,ajax,Php,Ajax,我已经成功地编写了一个可用的Ajax代码,但问题是我只能添加一个输入字段一次。我有一个提交输入,它调用Ajax脚本,一切正常,输入文本字段出现,但在此之后,如果我想通过单击提交输入添加另一个文本字段,它就不起作用了。您只能加载一次(YOLO)。如何编写更棒的代码,让我根据需要添加尽可能多的文本字段?谢谢你的回复。这是我的密码: index.php: <head> <script> function ajaxobj(){ if (window.XMLHttpReques

我已经成功地编写了一个可用的Ajax代码,但问题是我只能添加一个输入字段一次。我有一个提交输入,它调用Ajax脚本,一切正常,输入文本字段出现,但在此之后,如果我想通过单击提交输入添加另一个文本字段,它就不起作用了。您只能加载一次(YOLO)。如何编写更棒的代码,让我根据需要添加尽可能多的文本字段?谢谢你的回复。这是我的密码:

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>

函数ajaxobj(){
if(window.XMLHttpRequest){
xmlhttp=新的XMLHttpRequest();
}否则{
xmlhttp=newActiveXObject('Microsoft.xmlhttp');
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById('asd').innerHTML=xmlhttp.responseText;
}   
}
open('GET','ajaxAddInput.php',true);
xmlhttp.send();
}
php文件:

<?php
echo '<input type=\"text\">';
?>

只需使用标准化库,就像。考虑到这一点,您可以使用以下代码:

$("#asd").on('change', function() {
    var val = $(this).val();
    $.ajax("ajaxAddInput.php", {input: val});
});
或者,您也可以将库与类一起使用:

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

谢谢你的回复。是的,我知道jquery,但我听说在开始使用jquery之前,我应该先学习经典的Ajax。经典的Ajax现在过时了吗?虽然这可能是一个品味问题,但我更希望解决方案能够解决您需要处理的问题。为什么不使用一个为您提供不同浏览器方法的库呢?