简体   繁体   English

清除 javascript 中的 div

[英]clear div in javascript

i'm new to javascript development and stuck with a problem: depending on the selected option in a form, I want first to clear the inner of a div and fill it then with another content by using ajax.我是 javascript 开发的新手,遇到了一个问题:根据表单中选择的选项,我想首先清除 div 的内部,然后使用 ajax 填充其他内容。 This is my current code:这是我当前的代码:

$(document).on('change','#uebungsart', function() {
    var div = document.getElementById('zusatz-uebung');
    while(div.firstChild) {
        div.removeChild(div.firstChild);
    }
});
 
$( document ).ready(function() {
    if(document.getElementById('uebungsart').value == "bet") {  
    $.ajax({  
            type: 'POST',  
            url: 'helper/javascript-loadings/addOMT.php', 
            success: function(response) {
                $("#zusatz-uebung").html(response);   
            }
        });
    }

    if(document.getElementById('uebungsart').value == "omt") {
    $.ajax({  
            type: 'POST',  
            url: 'helper/javascript-loadings/addBET.php', 
            success: function(response) {
                $("#zusatz-uebung").html(response);
                }
            });
    }      
});

Unfortunately that doesn't work... may somebody help me?不幸的是,这不起作用......有人可以帮助我吗? :-) :-)

best regards Florian最好的问候弗洛里安

Thank you guys for your hints, I edited my code to the following:谢谢大家的提示,我将代码编辑为以下内容:

$(document).on('change','#uebungsart', function() {
           document.getElementById('zusatz-uebung').innerHTML = "";
            if(document.getElementById('uebungsart').value == "bet") {
                $.ajax({  
                    type: 'POST',  
                    url: 'helper/javascript-loadings/addOMT.php', 
                    success: function(response) {            
                        $("#zusatz-uebung").html(response);
                        }
                });
            }

            if(document.getElementById('uebungsart').value == "omt") {
                $.ajax({  
                    type: 'POST',  
                    url: 'helper/javascript-loadings/addBET.php', 
                    success: function(response) {
                        $("#zusatz-uebung").html(response);
                        }
                    });
                }
        });

Now it clears the content of the response-div correctly and loads the response of both php-Scripts depending on the selected option现在它会正确清除 response-div 的内容并根据所选选项加载两个 php-Scripts 的响应

Try this:尝试这个:

<form action="">
 <label for="fname">DATA:</label>
 <input type="text" id="fname" name="fname" onkeyup="datawithquery(this.value)">
</form>
<p>User: <span id="show"></span></p>

function datawithquery(id) {
  if (id.length == 0) {
  document.getElementById("fname").placeholder = "none data";
return;
}
else {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
    document.getElementById("show").innerHTML = this.responseText;
  }
}
xmlhttp.open("POST", "helper/javascript-loadings/addBET.php?q="+id, true);
xmlhttp.send();

} } } }

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

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