How to trigger an AJAX request for a date change event?

index.php

<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" src="ajax.js"></script>

<a href='one.php' class='ajax'>One</a>
<a href='two.php' class='ajax'>Two</a>

<div id="workspace">workspace</div>

one.php

$arr = array ( "workspace" => "One" );
echo json_encode( $arr );

two.php

$arr = array( 'workspace' => "Two" );
echo json_encode( $arr );

ajax.js

jQuery(document).ready(function(){
    jQuery('.ajax').live('click', function(event) {
        event.preventDefault();
        // load the href attribute of the link that was clicked
        jQuery.getJSON(this.href, function(snippets) {
            for(var id in snippets) {
                // updated to deal with any type of HTML
                jQuery('#' + id).html(snippets[id]);
            }
        });
    });
});

It works fine on code. When I click the One link, then the One line is loaded into the DIV workspace, and when I click the Two link, then the Two line is loaded into the DIV workspace.

Question:

Now I want to use the drop-down list to load one.php and two.php into the DIV workspace instead of the links in index.php. When I use a link, I use class = 'ajax' in the link properties, but how do I call an ajax request when a change event changes?

thank

+3
source share
2 answers

:

jQuery('#dropdown_id').live('change', function(event) {
    jQuery.getJSON($(this).val(), function(snippets) {
        for(var id in snippets) {
            // updated to deal with any type of HTML
            jQuery('#' + id).html(snippets[id]);
        }
    });
});

:

<select id="dropdown_id">
  <option value="one.php">One</option>
  <option value="two.php">Two</option>
</select>
+3

href . ajax.

"ajax" (, ).

0

Source: https://habr.com/ru/post/1758489/


All Articles