欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php级联

程序员文章站 2024-01-19 11:02:22
...
Demo

Fill second (or more)

-- Select Value -- Dog Cat Domestic Medium Hair

jQuery
$(document).ready(function(){     $("select#category").change(function(){         // Post string         var post_string = "type=" + $(this).val();          // Send the request and update sub category dropdown         $.ajax({             type: "POST",             data: post_string,             dataType: "json",             cache: false,             url: 'json.php',             timeout: 2000,             error: function() {                 alert("Failed to submit");             },             success: function(data) {                  // Clear all options from sub category select                 $("select#sub_category option").remove();                  // Fill sub category select                 $.each(data, function(i, j){                     var row = "";                     $(row).appendTo("select#sub_category");                 });             }         });     });     });
HTML
JSON AJAX Script, json.php
$json = array();  if ($_POST['type'] == 1) {     $json[] = array(         'value' => '1',         'text' => 'Staffordshire Bull Terrier'     );     $json[] = array(         'value' => '2',         'text' => 'Labrador Retriever/American Pit Bull Mix'     );     $json[] = array(         'value' => '3',         'text' => 'German Short Hair Pointer'     ); } elseif ($_POST['type'] == 2) {     $json[] = array(         'value' => '4',         'text' => 'Domestic Medium Hair'     ); }  echo json_encode($json);
相关标签: php级联