如何使用JQuery和PHP获取数组的值

时间:2017-04-24 18:57:02

标签: javascript php jquery

<div class="row">
                    <div class="col-sm-4 ">
                                        <label>Date of Birth:</label>

                    </div>

                    <div class="row">

                    <div class="col-sm-4">
                        <?php
                            // set start and end year range
                            $dayArray = range(01, 31);
                            ?>
                            <!-- displaying the dropdown list -->
                            <select id="dob[]" class="form-control">
                                <option value="">Date</option>
                                <?php
                                foreach($dayArray as $day) {
                    $selected = (day == 01) ? 'selected' : '';
                                    echo '<option '.$selected.'value="'.$day.'">'.$day.'</option>';
                                }
                                ?>
                            </select>
                    </div>
                    <div class="col-sm-4">
                        <select id="dob[]" class="form-control">
                                <option selected="selected">Month</option>
                                <?php

                                $month = array("Jan", "Feb", "Mar", "Apr", "May",  "Jun","Jul","Aug","Sep","Oct","Nov","Dec" );             
                                foreach($month as $item){
                                ?>
                                <option value="<?php echo strtolower($item); ?>"><?php echo $item; ?></option>
                                <?php
                                }
                                ?>
                            </select>
                    </div>
                    <div class="col-sm-4">
                        <?php

                            $yearArray = range(1960, 2050);
                            ?>

                            <select id="dob[]" class="form-control">
                                <option value="">Year</option>
                                <?php
                                foreach ($yearArray as $year) {
                                    // if you want to select a particular year
                                    $selected = ($year == "") ? 'selected' : '';
                                    echo '<option '.$selected.' value="'.$year.'">'.$year.'</option>';
                                }
                                ?>
                            </select>
                        </div>
                        </div>
                    </div>
                    </div>

我需要使用jquery获取数组的值, 如何使用dob[];

获取数组$("#dob").val()的值

尝试使用json_encode,json_decode无效。 当我试图提示val显示为未定义时。

1 个答案:

答案 0 :(得分:0)

首先,您设置id=dob[]的错误HTML应该是name=[]

&#13;
&#13;
$(document).ready(function()
    {
        $("#getValues").click(function()
        {
            var month = $("#month").val();
            var year = $("#year").val();
            var dob = month + " " + year;
            alert(dob);
            
        });
    });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    <div class="col-sm-4">
      <select name="dob['name']" class="form-control" id="month">
           <option selected="selected" disabled>Month</option>          
           <option value="1">Jan</option>
           <option value="2">Feb</option>
           <option value="3">Mar</option>
      </select>
    </div>
    <div class="col-sm-4">
        <select name="dob['year']" class="form-control" id="year">
             <option selected disabled>Year</option>
             <option value="2001">2001</option>
             <option value="2002">2002</option>
             <option value="2003">2003</option>
        </select>
        
        <button id="getValues">Get Values</button>
    </div>
&#13;
&#13;
&#13;

这是我给出的基本代码!希望您明白并继续编写代码