jQuery array() with select on change
I am trying to minimize my code by putting it into an array but nothing happens. I can't figure out what I am doing wrong. Here's the code
<html>
    <head>
    <title>test</title>
    <!-- JavaScript -->
    <script src="js/jquery-1.5.2.js" type="text/javascript"></script>   
    <script type="text/javascript">
        var phpf开发者_如何学Goile = new Object();
        phpfile["testselect"] = "zoomchange.php";
        var elementID = new Object();
        elementID["testselect"] = "#testdiv";
        $(document).ready(function(){
            $("select").change(function() {
              $.post(
                phpfile[$(this).id()],
                $(this).serialize(),
                function(data) {
                  $(elementID[$(this).id()]).html(data)
                }
              );
            });
        });
    </script>   
    </head>
    <body>
    <select id="testselect">
        <option value="1">1</option>
        <option value="2">2</option>
    </select>
    <div id="testdiv"></div>
    </body>
</html>
here is the zoomchange.php:
<?PHP echo $_REQUEST['testselect'] ; ?>
Your initializers shouldn't look like this:
var phpfile = new Array();
phpfile["testselect"] = "zoomchange.php";
var elementID = new Array();
elementID["testselect"] = "#testdiv";
A JavaScript Array is indexed by numbers, not strings. You want simple object literals:
var phpfile   = { testselect: 'zoomchange.php' };
var elementED = { testselect: '#testdiv'       };
Then, your POST callback is confused:
function(data) {
    $(elementID[$(this).id()]).html(data)
}
this isn't what you think it is when that function is called. You want something more like this:
$("select").change(function() {
    var that = this;
    $.post(
        phpfile[that.id],
        $(this).serialize(),
        function(data) {
            $(elementID[that.id]).html(data);
        }
    );
});
This
function(data)
{
  $(elementID[$(this).id()]).html(data);
}
instead of this
function(data)
{
  $(elementID[$(this).id()]).html(data)
}
Is this the error ?
You should do new Object() instead of new Array().
Edit: There are other mistakes, your js code should be this:
<script type="text/javascript">
        var phpfile = {};
        phpfile["testselect"] = "zoomchange.php";
        var elementID = {};
        elementID["testselect"] = "#testdiv";
        $(document).ready(function(){
            $("select").change(function() {
              var $select = $(this);
              $.post(
                phpfile[$select.attr("id")],
                $select.serialize(),
                function(data) {
                  $(elementID[$select.attr("id")]).html(data)
                }
              );
            });
       });
</script>
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论