我使用一些Jquery / ajax代码根据select的值刷新div的内容。所以我有这个:
<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是:
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;
}
这很好用。
但是这里是“ 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>
如果输入电子邮件为空,则脚本应隐藏输入文本。当我第一次加载页面时(警告“ tada”显示),它工作得很好,但是从ajax调用中调用时却不能。你能帮我找到原因吗?提前致谢。
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();
}
}
---------------新代码块1 -----------------
//在发送到服务器之前,使用它来检查您的电子邮件输入框是否为空值
<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>
---------------新代码块2 -----------------
//这就是我建议发送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>
---------------新代码块3 -----------------
//从头开始隐藏所有btns(您也可以由此推断出如何分别隐藏每个单独的元素)
$(document).ready(function(){
$(".form-control").each(function(){
$(this)
.parent()
.next("input.btn")
.hide()
.next("input.btn-danger")
.hide();
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句