简体   繁体   English

在单击“选择选项”时,使下拉菜单消失

[英]On Clicking Select Option, make dropdown dissapear

I have a couple form elements that when clicked update the database and disappear. 我有几个表单元素,当单击它们时会更新数据库并消失。

At first, I have a button that reads Check In . 首先,我有一个显示Check In的按钮。 Upon clicking it, the database is updated and a dropdown is presented in place of the button. 单击数据库后,将更新数据库,并显示一个下拉列表来代替按钮。 In the dropdown, there are locations for the user to choose, that have values of their corresponding location-number, which upon clicking update the database. 在下拉列表中,有用户可以选择的位置,这些位置具有其对应的位置编号的值,单击后即会更新数据库。 The last option is labeled Check Out , and upon clicking it, the database is supposed to be updated one last time, and then red text saying Checked Out should appear. 最后一个选项标记为Check Out ,单击它时,该数据库应该是最后一次更新,然后出现红色文本,表示Checked Out

Here's my code: 这是我的代码:

<button class="checkIn">Check In</button>

<form method='post' class='myForm' action=''>

<td>
<select name='locationSelect' class='locationSelect'>
    <option value='1'>Exam Room 1</option>
    <option value='2'>Exam Room 2</option>
    <option value='3'>Exam Room 3</option>
    <option value='4'>Exam Room 4</option>
    <option value='CheckOut'>Check Out</option>
</select>
</form>

and here is the jquery 这是jQuery

<script src="http://code.jquery.com/jquery-1.8.2.js"></script>

<script type="text/javascript">
$(document).ready(function() {    
    $('.locationSelect').hide();
    $('.finished').hide();
});

$('.checkIn').click(function(){
    $e = $(this);
    $.ajax({
    type: "POST",
    url: "changeloc.php",
    data: "checkIn="+$(this).val(),
    success: function(){
      $('.checkIn').css("display","none");
      $('.locationSelect').show();

    }
    });
});

$('.locationSelect').change(function(){
    $e = $(this);
    $.ajax({
       type: "POST",
       url: "changeloc.php",
       data: "locationSelect="+$(this).val(),
       success: function(){

       }
    });
});
$('.locationSelect option[value="CheckOut"]').click(function(){
    $e = $(this);
    $.ajax({
       type: "POST",
       url: "changeloc.php",
       data: "checkOut="+$(this).val(),
       success: function(){
       $('.locationSelect').css("display","none");
       $('.finished').show();
       alert('done');
       },
       error: function(request){
       alert(request.responseText);
       }
    });
});

</script>

The problem is that everything works until the user hits Check Out , and then red text doesn't appear and the dropdown doesn't disappear. 问题在于,一切正常,直到用户单击Check Out ,然后红色文本不会出现并且下拉菜单也不会消失。 What's the problem? 有什么问题?

Thanks for any and all help! 感谢您提供的所有帮助!

Having the check out button in a drop down select list isn't right. 在下拉选择列表中使用“ 签出”按钮是不正确的。 These lists are for options , not for commands. 这些列表用于选项 ,而不用于命令。

Why don't you have a checkout button that following the list, and is hidden 为什么您没有在清单后面的结帐按钮被隐藏

<button class="checkOut" style="display:none;" value="Check Out" />

This way, once your room list has been populated, you can simply set the check out button to visible again. 这样,一旦您的房间列表被填充,您就可以简单地将“退房”按钮设置为再次可见 This means the page logics flows (checkin - select - checkout). 这意味着页面逻辑流(签入-选择-签出)。

For the red text, replace the dropdown with something like this? 对于红色文字,将下拉列表替换为类似的内容?

$(".checkOut").click(function(e) {
  $(".locationSelect").html("<p style='color:red'>Checked out!</p>");
});

Just food for thought. 值得深思。

Alternatively, you could check for the selected option in the $('.locationSelect').change() handler. 或者,您可以在$('.locationSelect').change()处理程序中检查选定的选项。

$('.locationSelect').change(function(e) {
  if($(this).children(":selected").val() === "CheckOut") {
    // Perform checkout logic.
  }
  else {
    // Perform room select logic.
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选择下拉菜单,使默认值不是可选选项 - Select Dropdown, make default Value not a selectable option 如何制作选择框的下拉选项 - How to make the dropdown option of a select box 自动 select jquery select2中的选项,如果下拉菜单中只有一个选项,无需单击它,在页面加载 - Automatically select the option in jquery select2, if only one option is present in dropdown, without clicking it, on page load 如何在链式下拉选项中使二元输入选择选项? - How to make dimanic input select option in chained dropdown option? 单击链接时如何使我的汉堡菜单消失 - How to make my burger menu dissapear when clicking on link 单击选择元素,然后单击选项 - clicking select element and then clicking an option 如何制作选择下拉列表以根据当前时间选择选项值 - How make a select dropdown to select an option value based on current time 单击后如何在SELECT多行中进行选择? - How can I make an option in a SELECT multi line upon clicking on it? 如何在下拉列表中的搜索栏中添加href选择选项? - How to make a href appended in search bar select option in dropdown? 是否有可能通过CSS将选择下拉列表的选项显示为optgroup - Is it possible to make an option of a select dropdown appear as optgroup via css
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM