使用Javascript在PHP中选择选项时启用按钮

使用Javascript在PHP中选择选项时启用按钮,javascript,jquery,Javascript,Jquery,我尝试了其他主题中的一些代码来解决我的问题,但我仍然无法得到我想要的 我需要一个按钮,当用户在选择选项中选择一个项目时,该按钮变为启用,当用户重新选择默认选择时,该按钮变为禁用 <script text="text/javascript"> $(document).ready(function(){ $('#select_resa').change(function(){ var val = $('#select_resa').val();

我尝试了其他主题中的一些代码来解决我的问题,但我仍然无法得到我想要的

我需要一个按钮,当用户在选择选项中选择一个项目时,该按钮变为启用,当用户重新选择默认选择时,该按钮变为禁用

    <script text="text/javascript">

$(document).ready(function(){
    $('#select_resa').change(function(){
        var val = $('#select_resa').val();
        alert(val);

        if (val == '') {
            $('#supprimer_resa_button').attr('disabled', 'disabled');
        }else{
            $('#supprimer_resa_button').removeAttr('disabled');
        }
    });
});
</script>

<form method="post" action="modif_resa.php?" id="modif_resa">
    <h2>Modifier/Supprimer un reservation</h2>
    <?php if(isset($erreur)) echo '<p style="font-style:italic;font-size:0.8em;text-align:center;color:red;margin-bottom:5px;">'.$erreur.'</p>'; ?>



    <label for="salle"><strong>Choisissez une reservation</strong></label>

    <select id="select_resa" name="select_resa">
        <option value="" id="select" name="select">--Selectionnez--</option>
        <option value=""></option>
        <option value="2">salut</option>
        <?php
        if(!empty($liste_reservations)){
            for($i=0;$i<count($liste_reservations);$i++){
                echo '<option "title="'.$liste_reservations[$i]['RS_NOM'].'" value="'.$liste_reservations[$i]['RS_NOM'].'" '.(isset($SALLE) && $SALLE==$liste_reservations[$i]['RS_ID']?'selected="selected"':'').'>'.$liste_reservations[$i]['RS_NOM'].'</option>';
            }
        }
        ?>
   </select>

   <br/><br/>


    <label for="s_date"> Modifier la date de fin</label>
    <input type="text" value="<?php if(isset($RS_DATE)) echo $RS_DATE;?>" name="s_date" id="s_date"  maxlength="10" size="10"/>
    <a href="javascript:void(0);"   onclick="javascript:return CalendarDate('s_date');"><img src="/Libs/calendar/calendar_small.png" alt="Calendrier" style="vertical-align:middle; border:0;" title="Calendrier" /></a>
    <br/><br/> 

    <span class="valider">
    <input type="submit" name="valider" value="<?php echo TXT_VALIDER; ?>" />

    </span>

        <br/><br/>


    <div style="text-align:center; margin-top: 50px;">
    <input id="supprimer_resa_button" disabled="disabled" type="submit" name="supprimer_resa_button" style="font-size: 15px; width: auto; height:auto; font-weight:bold;" value="<?php echo TXT_SUPPRIMER_RESA; ?>" />
    </div>

</form>
我的提交按钮在任何情况下都处于启用状态。。。 我错在哪里

您只需使用:

$('#supprimer_resa_button').removeAttr('disabled');
而不是

$('#supprimer_resa_button').attr('disabled', '');

在最近的jQuery api中,必须在布尔html属性上使用prop:

$('#supprimer_resa_button').prop('disabled', false)

attr仅具有可以包含值的html属性

在您的情况下,您还可以做得稍微短一点:

function updateFormEnabled() {
    $('#supprimer_resa_button').prop('disabled', verifyAdSettings());
}
请检查以下内容:


与php完全无关,这是Javascript/jQuery。在值更改时启用/禁用按钮方面,与php仍然无关。谢谢回答!之前是否需要在按钮上添加属性disabled=disabled?或者我让你按照上面的代码来做?如果按钮在默认情况下被禁用,你必须添加它。如果您使用的是xhtml,则使用disabled=disabled。仅当您在HTMLW中时才禁用。为什么OP应检查此项?请详细说明您的答案是如何成为解决方案的,以便对将来访问此问题的用户有用。当然。当我点击我的一个项目时,它仍然会被禁用,即使我点击了def,例如,也许我在如何将javascript放到表单之外的问题上错了?你能查一下吗。。我更新了我的代码,我会检查你的更新代码谢谢你插入jQuerymin.js了吗
function updateFormEnabled() {
    $('#supprimer_resa_button').prop('disabled', verifyAdSettings());
}
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

<script text="text/javascript">
$(document).ready(function(){
    $('#select_resa').change(function(){
        var val = $('#select_resa').val();

        if (val == '') {
            $('#supprimer_resa_button').attr('disabled', 'disabled');
        }else{
            $('#supprimer_resa_button').removeAttr('disabled');
        }
    });
});



</script>

<form method="post" action="modif_resa.php?" id="modif_resa">
    <h2>Modifier/Supprimer un reservation</h2>
    <?php if(isset($erreur)) echo '<p style="font-style:italic;font-size:0.8em;text-align:center;color:red;margin-bottom:5px;">'.$erreur.'</p>'; ?>



    <label for="salle"><strong>Choisissez une reservation</strong></label>

    <select id="select_resa" name="select_resa">
        <option value="" id="select" name="select">--Selectionnez--</option>
        <option value="1">abc</option>
        <option value="2">def</option>
        <option value="3">ghi</option>
        <?php
        if(!empty($liste_reservations)){
            for($i=0;$i<count($liste_reservations);$i++){
                echo '<option "title="'.$liste_reservations[$i]['RS_NOM'].'" value="'.$liste_reservations[$i]['RS_ID'].'" '.(isset($SALLE) && $SALLE==$liste_reservations[$i]['RS_ID']?'selected="selected"':'').'>'.$liste_reservations[$i]['RS_NOM'].'</option>';
            }
        }
        ?>
   </select>

   <br/><br/>


    <label for="s_date"> Modifier la date de fin</label>
    <input type="text" value="<?php if(isset($RS_DATE)) echo $RS_DATE;?>" name="s_date" id="s_date"  maxlength="10" size="10"/>
    <a href="javascript:void(0);"   onclick="javascript:return CalendarDate('s_date');"><img src="/Libs/calendar/calendar_small.png" alt="Calendrier" style="vertical-align:middle; border:0;" title="Calendrier" /></a>
    <br/><br/>

    <span class="valider">
    <input type="submit" name="valider" value="<?php echo TXT_VALIDER; ?>" />

    </span>

        <br/><br/>


    <div style="text-align:center; margin-top: 50px;">
    <input id="supprimer_resa_button" disabled="disabled" type="submit" name="supprimer_resa_button" style="font-size: 15px; width: auto; height:auto; font-weight:bold;" value="<?php echo TXT_SUPPRIMER_RESA; ?>" />
    </div>

</form>