如何检查是否从<select>中选择了一个值?

But*_*nut 0 php jquery select

我正在尝试更新<div><select>元素中选择新值时的内容,但我当前的代码不起作用.使用我当前的代码,每次单击时都会更新div <select>.我该如何解决这个问题?

这是我的js代码:

// Month PROCESS 
$(function(){
    $('select[name=month]').click(function(e) {
        $('<img src="../images/loading.gif" id="loading" style="width:auto;" />').appendTo("#mes");

        //Cancel the link behavior
        e.preventDefault();
        //Get the A tag

        var month = $( 'select[name=month]' ).val();

        $.ajax({
            type:"GET",
            url:"pages/daily-process.php",
            data: {mid: month},
            dataType: 'html',
            target: '#dateProcess',
            success: function(data){
                $("#mes").find('img#loading').remove();
                $("#mes").html(data);
            }
        })

    }); 
});
Run Code Online (Sandbox Code Playgroud)

和HTML:

<div id="dateProcess">
<select name="month">
    <option value="1">January</option>
    <option value="2">February</option>
    <option value="3">March</option>
    <option value="4">April</option>
    <option value="5">May</option>
    <option value="6">June</option>
    <option value="7">July</option>
    <option value="8">August</option>
    <option value="9">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>
</select>
<div id='mes'></div>
</div>
Run Code Online (Sandbox Code Playgroud)

这是daily-process.php

<?php

    $getMonth = $_GET['mid'];
    $getYear = $_GET['yid'];

    $begin = new DateTime( $getYear.'-'.$getMonth.'-01' );
    $end = new DateTime( $getYear.'-'.$getMonth.'-31' );
    $end = $end->modify( '+1 day' );

    $interval = new DateInterval('P1D');
    $daterange = new DatePeriod($begin, $interval ,$end);

    foreach($daterange as $date){
        echo $date->format("F d, Y") . "<br />";
    }
?>
Run Code Online (Sandbox Code Playgroud)

bfa*_*tto 8

如果我正确地回答了问题,那么您使用的是错误的事件.

您应该使用change而不是click:

$('select[name=month]').change( //...
Run Code Online (Sandbox Code Playgroud)