进行更改后刷新div内容

时间:2012-09-21 08:44:13

标签: php javascript ajax jquery

以下是我的代码,工作正常......

<Script type="text/javascript">
function im()
{
    var Name=document.getElementById("Name").value;
    var pointsize=document.getElementById("pointsize").value;
    var format=document.getElementById("format").value;
    var bckclr=document.getElementById("bckclr").value;
    var color=document.getElementById("color").value;
    var bcolor=document.getElementById("bcolor").value;
    var font=document.getElementById("font").value;

    $(document).ready(function(){
        var url = 'Name='+Name+'&pointsize='+pointsize+
                  '&format='+format+'&bckclr='+bckclr+
                  '&color='+color+'&bcolor='+bcolor+
                  '&font='+font;

        alert(url);
        //-----Sending request to server for getting job name list by ajax-----
        $.ajax({
            type    : "POST",
            url : "i.php?",
            data    : url,              
            cache   : false,
            success : function(html) {
                //document.getElementById('Div_PJobId').style.display="block";
                //alert('hi');
                alert(html);
                var pic='<img src="'+html+'">';
                $("#Div_Im").html(pic).show();
            }
        });
    });
}
</script>

和php代码......

echo('Name: <input type="text" id="Name" onchange="im()" value="your name"  name="Name" />');

echo('pointsize: <input type="text" id="pointsize" onchange="im()" value="50" name="pointsize" />');

echo('format: <input type="text" id="format" value=".gif" onchange="im()" name="format" />');

echo('BackGround Color: <input type="text" id="bckclr" value="red" onchange="im()" name="bckclr" />');

echo('FontColor: <input type="text" id="color" value="white" onchange="im()" name="color" />');

echo('Border Color: <input type="text" id="bcolor" value="blue" onchange="im()" name="bcolor" />');

echo('<a href="./lang/ims.php"><img src="'.$image.'" height="82" width="82" /></a>');

echo('Font: <input type="text" id="font" value="'.$image1.'" onchange="im()" name="font" />');

echo ('<div id="Div_Im">');
echo('replace me');
echo ('</div>');

正如你所看到的,我正在使用AJAX从服务器获取数据并在其正常工作中显示,但我每次进行更改时都需要刷新div。

当我做一些更改时如何刷新div ...当数据来自服务器时我还需要添加符号或加载图像......怎么做?

2 个答案:

答案 0 :(得分:0)

也许试试这个:

的Javascript

<script type="text/javascript">
$(document).ready(function(){

  $('#theIMform input')
    .change(function(){
      // Automatically creates a Query String for all fields within the parent form
      formdata = $(this).closest('form').serialize();

      $.ajax({
        type : "POST" ,
        url : "i.php" ,
        data : formdata ,              
        cache : false ,
        error : function( jqXHR , textStatus , errorThrown ){
          // Something went wrong
        } ,
        success : function( data , textStatus , jqXHR ){
          $( "#div_im" )
            .hide()
            .html( '<img src="'+data+'">' )
            .show();

        }
      });
    });

});
</script>

PHP

?>
<form id="theIMform" name="theIMform">
  Name: <input type="text" id="Name" value="your name"  name="Name" />
  pointsize: <input type="text" id="pointsize" value="50" name="pointsize" />
  format: <input type="text" id="format" value=".gif" name="format" />
  BackGround Color: <input type="text" id="bckclr" value="red" name="bckclr" />
  FontColor: <input type="text" id="color" value="white" name="color" />
  Border Color: <input type="text" id="bcolor" value="blue" name="bcolor" />
  <a href="./lang/ims.php"><img src="<?php echo $image; ?>" height="82" width="82" /></a>
  Font: <input type="text" id="font" value="<?php echo $image1; ?>" name="font" />
</form>
<div id="div_im">replace me</div>
<?php

答案 1 :(得分:-1)

当您更改页面元素时,它会自动刷新。

我认为你的问题是关于这一行

$("#Div_Im").html(pic).show();

因为您调用show给img元素而不是div。

尝试在

中更改它
$("#Div_Im").html(pic);
$("#Div_Im").show();

此外,我建议您使用jquery获取值,因此您需要更轻松地更改color=document.getElementById("color").value;