php 使用jQuery获取选择框中选定选项的值时出现问题

lmvvr0a8  于 2023-01-04  发布在  PHP
关注(0)|答案(1)|浏览(157)

我有下面的代码,试图从两个选定的框中获取变量与jQuery传递给PHP。所有似乎可以,但由于某种原因,我不能得到变量(JS错误:x1月1x)

<?php

// Handle AJAX request (start)
if( isset($_POST['ajax']) && isset($_POST['book']) && isset($_POST['chapter']) ){
  echo json_encode($_POST['book'] . " " . $_POST['chapter']);
  exit;
}
// Handle AJAX request (end)
?>

<!doctype html>
<html>

  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
        $(function() {

                $("#json-one").change(function() {

                        var $dropdown = $(this);

                        $.getJSON("jsondata/data.json", function(data) {

                                var key = $dropdown.val();
                                var vals = [];

                                switch(key) {
                                        case 'Genesis':
                                                vals = data.Genesis.split(",");
                                                break;
                                        case 'Exodus':
                                                vals = data.Exodus.split(",");
                                                break;
                                        case 'Leviticus':
                                                vals = data.Leviticus.split(",");
                                                break;
                                        case 'Numbers':
                                                vals = data.Numbers.split(",");
                                                break;
                                        case 'Deuteronomy':
                                                vals = data.Deuteronomy.split(",");
                                                break;
                                        case 'base':
                                                vals =  ['Select book'];
                                }

                                var $jsontwo = $("#json-two");
                                $jsontwo.empty();
                                $.each(vals, function(index, value) {
                                        $jsontwo.append("<option>" + value + "</option>");
                                });

                        });
                });

        });
    </script>
  </head>
  <body >

   <form method='post' action>
   <select name='book' class='book' id='json-one'>
      <option selected value="base">- Select Book -</option>
      <option value="Genesis">Genesis</option>
      <option value="Exodus">Exodus</option>
      <option value="Leviticus">Leviticus</option>
      <option value="Numbers">Numbers</option>
      <option value="Deuteronomy">Deuteronomy</option>
   </select>

   <br />

   <select name='chapter' class='chaper' id='json-two'>
      <option>Please choose from above</option>
   </select>

   <input type='button' name='submit' value='click' id='but' />
   </form>
   <!-- Script -->
   <script>
   $(document).ready(function(){
      $('#but').click(function(){

         $("select.book").change(function () {
            var selectedBook = $(this).children("option:selected").val();
         });

         $("select.chapter").change(function () {
            var selectedChapter = $(this).children("option:selected").val();
         });

         if(selectedBook.length > 0 && selectedChapter.length > 0){
            $.ajax({
               type: 'post',
               data: {ajax: 1, book: selectedBook, chapter: selectedChapter},
               dataType: 'json',
               success: function(response){
                   $('#response').text('response : ' + JSON.stringify(response) );
               }
            });
         }

      });
   });
   </script>
  </body>
</html>

我的jsondata/data.json如果重要的话是:

{
        "Genesis": "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50",
        "Exodus": "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40",
        "Leviticus": "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27",
        "Numbers": "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36",
        "Deuteronomy": "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34"
}
k3fezbri

k3fezbri1#

$(document).ready(function(){
      $('#but').click(function(){

         $("select.book").change(function () {
            var selectedBook = $(this).children("option:selected").val();
         });

         $("select.chapter").change(function () {
            var selectedChapter = $(this).children("option:selected").val();
         });

         if(selectedBook.length > 0 && selectedChapter.length > 0){
            $.ajax({
               type: 'post',
               data: {ajax: 1, book: selectedBook, chapter: selectedChapter},
               dataType: 'json',
               success: function(response){
                   $('#response').text('response : ' + JSON.stringify(response) );
               }
            });
         }

      });
   });

var selectedBook在可见性范围之外-在函数内部声明的变量在函数外部不可见

相关问题