我正试图使用select form
中的jQuery AJAX
在另一个php页面中包含一个外部php(在我自己的服务器上)。
基本上,如果用户从dropdown select form
中选择YES
选项,我想让用户包括external php
文件,如果用户选择NO
,我想删除它。
我有以下代码:
<html>
<head>
<script>
$('#test2').change(function(){
var selectedValue = $(this).val();
if (selectedValue === 'Yes') {
$("#content").load("include.php");
} else {
//WHAT DO I NEED TO PUT HERE TO REMOVE THE "include.php" if it has been inlcluded?
}
});
</script>
</head>
<body>
<form>
<select id="test2">
<option value=""></option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
</form>
<br>
<div id="content"></div>
</body>
</html>
第一个问题:我这样做对吗?
第二个问题:如果他们选择NO option
,我需要做什么才能删除include.php
文件?
任何帮助都将不胜感激。
提前谢谢。
附言:我的网页中确实包含jquery。
else块中的$('#comment').html('')
是正确的。但是,if条件错误(.val()
返回的值不是所选选项的文本)。用途:
$('#test2').change(function(){
var selectedValue = $(this).val();
if (1 == selectedValue) {
$("#content").load("include.php");
} else {
$('#content').html('');
}
});
If yes is selected then
Include file
else
$( ".page" ).empty();
//make the page content empty Simple
您的代码看起来正在请求include.php。您可以通过调用来清除加载的数据
$('#content').html('');
在else
部分