这是对上一个问题的跟进。
我有一个图标单击它时图标更改并调用go.php?go=
当您再次单击它时图标恢复为原始图标并调用go.php?stop=
这行得通。目前 go.php 只是将正确的信息写入文本文件,理想情况下,我希望看到 go.php 的输出反映到具有图标的父页面上的 DIV 中。
我怎么做 ?
这是我到目前为止所拥有的..
<script language="javascript">
$(function () {
$('a.tip').on(' click', function () {
var $this = $(this),
container = $('#x')
prevHTML = container.html(),
req = {};
if ( $this.hasClass('go') ) {
$this.find('img').attr('src', 'images/go.gif');
$this.removeClass('go');
req = $.ajax({
url: 'go.php?go=' + $this.attr('id'),
type: 'get',
success: function ( data ) {
container.html(x);
$this.removeClass('go');
}
});
} else {
$this.find('img').attr('src', 'images/stop.gif')
.end().addClass('go');
req = $.ajax({
url: 'go.php?stop=' + $this.attr('id'),
type: 'get',
success: function ( data ) {
container.html( x );
$this.removeClass('go');
}
});
}
});
});
</script>
<a href='#' class='tip' id='4a' onclick=\"load('#');return false;\">
<img src='images/go.gif'>
</a>
<a href='#' class='tip' id='6a' onclick=\"load('#');return false;\">
<img src='images/go.gif'>
</a>
<a href='#' class='tip' id='8a' onclick=\"load('#');return false;\">
<img src='images/go.gif'>
</a>
go.php 包含:
<?php
$s = (isset($_REQUEST['go'])) ? "GO".$_REQUEST['go'] : "STOP".$_REQUEST['stop'];
$myFile = "TESTTEST.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $s;
fwrite( $fh, $stringData );
fclose( $fh );
echo $s;
?>
更新:这似乎工作:
<script language="javascript">
$(function () {
$('a.tip').on(' click', function () {
var $this = $(this),
container = $('#x')
prevHTML = container.html(),
req = {};
if ($this.hasClass('go')) {
$this.find('img').attr('src', 'images/ok.gif');
$this.removeClass('go');
req = $.ajax({
url: 'go.php?go=' + $this.attr('id'),
type: 'get',
success: function (data) {
container.html(data);
$this.removeClass('go');
}
});
} else {
$this.find('img').attr('src', 'images/error.gif')
.end().addClass('go');
req = $.ajax({
url: 'go.php?stop=' + $this.attr('id'),
type: 'get',
success: function (data) {
container.html(data);
}
});
}
});
});
</script>