Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
892 views
in Technique[技术] by (71.8m points)

javascript - How to make the message appear without typing in the form

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

#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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...