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 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题