[英]Execute javascript after AJAX call
I use some Jquery/ajax code to refresh the content of a div according to the value of a select. 我使用一些Jquery / ajax代码根据select的值刷新div的内容。 So i have this: 所以我有这个:
<select id="agence" name="agence" class="selectpicker scrollMe " data-style="btn-default" onchange="getContent('divmailsav')">
<option value="1">1</option>
<option value="2">2</option>
</select>
<div id="divmailsav">
<?php include 'formmail.php'; ?>
</div>
GetContent is: GetContent是:
function getContent(x){
var xhr=null;
if(window.XMLHttpRequest){xhr=new XMLHttpRequest();}
else if(window.ActiveXObject){xhr=new ActiveXObject("Microsoft.XMLHTTP");}
envoi = "formmail.php?agence="+$('#agence').val()+"&soc=<?php echo $_SESSION['SOC'] ?>";
xhr.open("GET",envoi,false);
xhr.send(null);
obj=document.getElementById(x);
obj.innerHTML =xhr.responseText;
}
This works pretty great. 这很好用。
But here is what's inside "formmail.php" 但是这里是“ formmail.php”里面的内容
<label for="inputEmail1" class="col-sm-2 control-label">Mail 1</label>
<div class="col-sm-6">
<input type="email" class="form-control" name="PMCPYMAIL1" id="inputEmail1" onkeyup="onkeyupmail(1,'')" placeholder="Email" value="<?php echo getmail('PMCPYMAIL1')?>">
</div>
<input type="text" value="Envoyer mail de test" id="buttonmail1" onclick="onclickmail(1,'');" class="btn btn-default col-sm-2">
<input type="text" value="Effacer" class="btn btn-danger btn-mini col-sm-1" id="amail1" onclick="deletemail(1,'')" style="margin-left: 35px;">
<div class="col-sm-offset-2 col-sm-9">
<input type="submit" value="Valider" class="btn btn-primary">
</div>
<script type="text/javascript">
alert('tada');
pageLoad = function(){
if ($('#inputEmail1' ).val() == ''){
$('#buttonmail1').hide();
$('#amail1').hide();
}
};
pageLoad();
</script>
The script should hide the input text if the input email is empty. 如果输入电子邮件为空,则脚本应隐藏输入文本。 It works well when i load the page for the first time (the alert 'tada' shows) but not when it's call from the ajax call. 当我第一次加载页面时(它会显示警报“ tada”),但当它是从ajax调用中调用时,效果很好。 Can you help me to find why please ? 你能帮我找到原因吗? Thanks in advance. 提前致谢。
function getContent(x){
var xhr=null;
if(window.XMLHttpRequest){xhr=new XMLHttpRequest();}
else if(window.ActiveXObject){xhr=new ActiveXObject("Microsoft.XMLHTTP");}
envoi = "formmail.php?agence="+$('#agence').val()+"&soc=<?php echo $_SESSION['SOC'] ?>";
xhr.open("GET",envoi,false);
xhr.send(null);
obj=document.getElementById(x);
if(xhr.responseText != ""){
obj.innerHTML =xhr.responseText;
}else{
$('#buttonmail1').hide();
$('#amail1').hide();
}
}
--------------- new code block 1 ----------------- ---------------新代码块1 -----------------
// use this to check your email input boxes for null values before sending to the server //使用此方法在发送到服务器之前检查电子邮件输入框中的空值
<script>
$(document).ready(function(){
$('form').click(function(){
var emptyVals = [];
$(".form-control").each(function(){
if($(this).val() == ""){
emptyVals.push($(this));
}
});
if(emptyVals.length > 0){
// don't send form one or more email values are empty
}
});
});
</script>
--------------- new code block 2 ----------------- ---------------新代码块2 -----------------
// this would be the way I'd recommend sending the AJAX (using jQuery) //这就是我建议发送AJAX的方式(使用jQuery)
<script>
$(document).ready(function(){
function getContent(){
$.ajax({
url:"formmail.php",
data:"agence="+$('#agence').val()+"&soc=<?php echo $_SESSION['SOC'] ?>"
}).done(function(data){
if(data!=""){
// checks to see if there is an empty string at the load of the content
}
})
}
});
</script>
--------------- new code block 3 ----------------- ---------------新代码块3 -----------------
// hide all btns from the start (you can probably also deduce from this how to hide each individual element seperately) //从头开始隐藏所有btns(您也可以由此推断出如何分别隐藏每个单独的元素)
$(document).ready(function(){
$(".form-control").each(function(){
$(this)
.parent()
.next("input.btn")
.hide()
.next("input.btn-danger")
.hide();
});
If you are set on using XMLHttpRequest manually instead of jQuery's $.Ajax() then you need to use "onreadystatechange": 如果您设置为手动使用XMLHttpRequest而不是jQuery的$ .Ajax(),则需要使用“ onreadystatechange”:
var xhr = null;
function getContent(x){
if(window.XMLHttpRequest){xhr=new XMLHttpRequest();}
else if(window.ActiveXObject){xhr=new ActiveXObject("Microsoft.XMLHTTP");}
envoi = "formmail.php?agence="+$('#agence').val()+"&soc=<?php echo $_SESSION['SOC'] ?>";
xhr.open("GET",envoi,false);
xhr.send(null);
xhr.onreadystatechange = handleResponse;
}
function handleResponse() {
var result;
if (xhr.readyState == 4) {
if (xhr.responseText != "") {
$('#' + x).html(xhr.responseText);
}
else {
$('#buttonmail1').hide();
$('#amail1').hide();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.