如何检查是否从<;选择>;


How to check if a value was selected from <select>?

当从<select>元素中选择新值时,我正在尝试更新<div>的内容,但我当前的代码不起作用。在我当前的代码中,每次单击<select>时都会更新div。我该怎么解决?

这是我的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);
            }
        })
    }); 
});

和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>

这是日常处理.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 />";
    }
?>

如果我正确地回答了问题,则表明您使用了错误的事件。

您应该使用change而不是click:

$('select[name=month]').change( //...