如何让信息出现而不在形式输入(How to make the message appear with

2019-11-04 13:31发布

我只是想后,我选择在下拉按钮的空间和时间显示的消息。 我选择了这一切,它会出现在这样的形式:

 $(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> 

上面的代码是index.php文件,里面那么当我把这个代码,它里面:

$("#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();
    });
});

和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";
}?>

此代码工作,但最终,该消息将没出现,除非我尝试插入或编辑的形式。

这是当我完成选择所有的空间和时间:

而当我输入的形式。

Answer 1:

#time_room的‘输入’事件将触发仅在手动输入,而不是当它的值是由JavaScript / jQuery的设定。

似乎没有任何意义附加一个事件处理#time_room作为它的价值将(或应该)只能用在应对变化#rooms#time被改变。

你需要引起$.post(...)时,要么被称为#rooms#time改变。

$(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();
});

请注意,我改变input更常见的change事件。



文章来源: How to make the message appear without typing in the form