dtsnx44260 2017-11-28 03:11
浏览 30
已采纳

如何在不在表单中键入的情况下显示消息

I just want to appear the message after I select the room and time in the drop down buttons. I select all of this, it will appear in the form like this:

$(document).ready(function(){ 
    $('#rooms, #time').bind('input', function() {        
        $('#time_room').val($('#rooms').val() + ' ' +
                            $('#time').val() );
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="rooms" id="rooms">
    <option value="" style="display: none;">SELECT</option>
    <option value="cas 104">cas 104</option>
    <option value="cas 105">cas 105</option>
</select>

<select name="time" id="time">
    <option value="" style="display: none;">SELECT</option>
    <option value="7:00 - 8:00 am">7:00 - 8:00 am</option>
    <option value="8:00 - 9:00 am">8:00 - 9:00 am</option>
</select>

<form action="" name="form">
    <input type="text" name="time_room" id="time_room">
</form>
<div id="feedback"></div>

The code above was inside of the index.php, then when I put this code inside of it:

$("#feedback").load("check.php").hide();
$("#time_room").on('input', function(){
    $.post("check.php", { time_room: form.time_room.value }, 
    function(result){
        $("#feedback").html(result).show();
    });
});

and inside of the check.php

<?php
$host = "localhost";
$user = "root";
$password = "";
$database = "subject_loading_for_csit";
$con = mysqli_connect($host, $user, $password, $database) or die ("could not connect");
if (mysqli_connect_errno()) {
    echo "connection failed:".mysqli_connect_error();
    exit;
}


$rmt = $_POST['time_room'];

$query =  mysqli_query($con, "SELECT * FROM subject_scheduled where room_time = '$rmt' ");
$count = mysqli_num_rows($query);

if ($count == 0) {
    echo "OK";
}else if($count > 0){
    echo "Already taken";
}?>

This codes works but eventually, the message won't shown up unless I try to insert or edit the form.

This is when I Finish to select all the room and time: This is when I Finish to select all the room and time:

And when I typed in the form. and when I type in the form

</div>
  • 写回答

1条回答 默认 最新

  • douduan2272 2017-11-28 03:42
    关注

    #time_room's 'input' event will fire only on manual input, not when its value is set by javascript/jQuery.

    There seems to be no point in attaching an event handler to #time_room as its value will (or should) only ever change in response to #rooms and #time being changed.

    You need to cause $.post(...) to be called when either #rooms or #time is changed.

    $(document).ready(function() {
        $('#rooms, #time').on('change', function() {
            var time_room_value = $('#rooms').val() + ' ' + $('#time').val();
            $('#time_room').val(time_room_value);
            $.post('check.php', { 'time_room': time_room_value }, function(result) {
                $("#feedback").html(result).show();
            });
        });
        // $("#feedback").load('check.php').hide(); // no point loading a message that will not be seen?
        $("#feedback").hide();
    });
    

    Note, I changed input to the more usual change event.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真