POST form HTML with Bootstrap Dropdown - html

In the following code, I want to submit a form with a Bootstrap dropdown and submit the selected value as "username". I'm not sure where in the following code I should add 'name="username"'. When I add it in certain places, it does not get set properly.
<form action="/login" method="POST">
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
Dropdown
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
</ul>
<input type="submit" value="Login">
</div>
</form>
Below is an example of code that does exactly what I want it to. But for design purposes, I'd like the form to be in Bootstrap.
<form action="/login" method="POST">
<select class="form-control" name="username">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<input type="submit" value="Login">
</form>

Replace the links in the list (the ones that currently link to the top of the page) with radio buttons and labels.

We can add radio buttons for every list item, each giving them a unique value and name="username". Wrap the text of every list item into a label with a for tag that points to the id of the corresponding radio button.
After that, the only thing that is left to do is wrapping the dropdown label into a span with id='userlabel', and use this script to change the label of the dropdown menu to the selected list item. This will give the user an idea of which item is currently selected:
$("input[name='username']").change(function(e){
$("#usernamelabel").text($(this).val());
});
We can hide the radio buttons with
input[name='username']{
display: none;
}
The resulting html will look like this:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<form action="/login" method="POST">
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<span id="usernamelabel">Select username</span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><input type="radio" id="username1" name="username" value="username1"><label for="username1">Username1</label></li>
<li><input type="radio" id="username2" name="username" value="username2"><label for="username2">Username2</label></li>
<li><input type="radio" id="username3" name="username" value="username3"><label for="username3">Username3</label></li>
</ul>
<input type="submit" value="Login">
</div>
</form>
<style>
input[name='username']{
display: none;
}
</style>
<script>
$("input[name='username']").change(function(e){
$("#usernamelabel").text($(this).val());
});
</script>

Related

How to bootstrap dropdown button with click function in knockout

i have a button dropdown with options and it works as i want, but i need to remove te input circle image in options, is it possible?
or any other idea? thanks!
<div class="dropdown">
<button class="btn btn-secondary btn-sm dropdown-toggle" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Menu 2</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item"><input type="radio" name="options" value="type1" autocomplete="off" data-bind="checked: type"> option 1 </a>
<a class="dropdown-item"><input type="radio" name="options" value="type2" autocomplete="off" data-bind="checked: type"> option 2 </a>
</div>
</div>
Just add this css rule :
input[type="radio"] {
visibility:hidden;
}

bootstrap input group select field,text field and search field

I want to build search bar similar to amazon site.
A select input field followed by input field followed by search button.
this is what I have got so far:
The issue is that the button1 dropdown is not a select input field if I do change to input field everything breaks.
examples in bootstrap site dont show group with input field select but dropdown menu.
my code
<div class="input-group input-group-lg">
<input type="text" name="query" class="form-control" placeholder="Search this site" />
<span class="input-group-btn">
<button type="button" class="btn btn-primary dropdown-toggle custom" data-toggle="dropdown">Button 1 <span class="caret"></span></button>
<ul class="dropdown-menu pull-right">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li class="divider"></li>
<li>Separated link</li>
</ul>
<button class="btn btn-primary" type="submit">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
css - to remove curve radius for center input field or buttons
.custom{
-webkit-border-radius: 0px !important;
-moz-border-radius: 0px !important;
border-radius: 0px !important;
}
Please have look at my demo. If you want add your style then add class to components to change there style.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
<div class="col-md-6">
<div class="input-group">
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Apps & Games <span class="caret"></span></button>
<ul class="dropdown-menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li role="separator" class="divider"></li>
<li>Separated link</li>
</ul>
</div><!-- /btn-group -->
<input type="text" class="form-control" aria-label="Amount (to the nearest dollar)">
<span class="input-group-addon"><span class="glyphicon glyphicon-search" aria-hidden="true"></span></span>
</div>
</div>
</div>
I got it solved just amazon like search using this:
<div class="input-group my-group">
<select id="filter_type" name="filter_type" class="selectpicker form-control" style="width:30%;">
<option value>Select Filter Type..</option>
<option value="all">All</option>
<option value="firstname">First Name</option>
<option value="lastname">Last Name</option>
<option value="email">Email</option>
</select>
<select id="search" name="search" class="selectpicker form-control" disabled="disabled" style="width:70%; display:block;">
<option value>select filter type first</option>
</select>
<input type="text" class="form-control" style="width:70%; display:none" name="searchfield" id="searchfield" placeholder="Enter username...">
<span class="input-group-btn">
<button id="submit" name="submit" class="btn btn-primary" disabled="disabled" type="submit">Filter</button>
</span>
</div>
Just right click the amazon search bar and check out the code behind.

How do I create a button group with a select, button, button-dropdown and addon with bootstrap

I am trying to create a a group of controls with bootstrap however it does not seem to be rendering properly, I have an example here: https://jsfiddle.net/brightertools/jxafnoyx/
Code:
<form id="form" name="form" action="" method="post">
<div class="container">
<div class="form-group">
<label class="control-label" for="SelectOption">Select an Option</label>
<div class="input-group">
<select id="SelectOption" name="SelectOption" class="form-control">
<option value="">(select your option)</option>
</select>
<div class="input-group-btn ">
<button type="button" class="btn btn-default glyphicon glyphicon-plus" style="margin-top:-1px;" id="AddButton"></button>
<button type="button" class="btn btn-default dropdown-toggle glyphicon glyphicon-cog" style="margin-top:-1px;" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" id="GroupDropdown"></button>
<ul class="dropdown-menu">
<li><a id="EditLink">Edit</a>
</li>
<li><a id="DeleteLink">Delete</a>
</li>
</ul> <span class="input-group-addon"><span class="validation-icon glyphicon glyphicon-asterisk"></span></span>
</div>
</div>
</div>
</div>
</form>
One can see that the addon is wrapping, I cannot seem to fix this issue.
Current rendering within jsfiddle:
As Onkar Deshpande mentioned correctly in his comment
"multiple form-controls in a single input group are not supported".
Input groups
Place one add-on or button on either side of an input. You may also place one on both sides of an input.
We do not support multiple add-ons on a single side.
We do not support multiple form-controls in a single input group.
But there is a little work around you could use to make this work.
Just make sure to move your input-group-addon out of the input-group-btn div.
To make this look correct you would have to also remove the border-radius on the .btn class.
.btn {
border-radius: 0 !important;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<form id="form" name="form" action="" method="post">
<div class="container">
<div class="form-group">
<label class="control-label" for="SelectOption">Select an Option</label>
<div class="input-group">
<select id="SelectOption" name="SelectOption" class="form-control">
<option value="">(select your option)</option>
</select>
<div class="input-group-btn ">
<button type="button" class="btn btn-default glyphicon glyphicon-plus" style="margin-top:-1px;" id="AdvertiserEditButton"></button>
<button type="button" class="btn btn-default dropdown-toggle glyphicon glyphicon-cog" style="margin-top:-1px;" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" id="AdvertiserDropdown"></button>
<ul class="dropdown-menu">
<li><a id="AdvertiserEditLink">Edit</a>
</li>
<li><a id="AdvertiserDeleteLink">Delete</a>
</li>
</ul>
</div>
<span class="input-group-addon"><span class="validation-icon glyphicon glyphicon-asterisk"></span></span>
</div>
</div>
</div>
</form>
You can try like this: Demo
<div class="input-group">
<select id="SelectOption" name="SelectOption" class="form-control">
<option value="">(select your option)</option>
</select>
<div class="input-group-btn">
<button type="button" class="btn btn-default "><i class="glyphicon glyphicon-plus"></i></button>
<button type="button" class="btn btn-default dropdown-toggle " data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" id="AdvertiserDropdown"><i class="glyphicon glyphicon-cog"></i></button>
<ul class="dropdown-menu dropdown-menu-right">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li role="separator" class="divider"></li>
<li>Separated link</li>
</ul>
</div>
<span class="input-group-addon validation-icon glyphicon glyphicon-asterisk"></span> </div>
<!-- /.input-group -->

Dropdown list isn't shown correctly! Why?

While clicking on the dropdown-list-button the dropdown list opens up but isn't shown correctly. Anybody has an idea to fix this?
<div class="btn-group">
<button data-toggle="dropdown" class="btn btn-default dropdown-toggle">
Checked option
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><input type="radio" id="ID" name="NAME" value="VALUE">
<label for="ID">Label</label>
</li>
<!-- Other items -->
</ul>
</div>
And I'm including these two Bootstrap files:
<script src="js/bootstrap.js"></script>
<link href="css/bootstrap.min.css" rel="stylesheet">
Like the others, here's a working example: http://codepen.io/emaildano/pen/zxopNj
Can you provide more detail on how this "isn't showing up correctly"?
If I had to guess you're referring to the width of the drop down. That's set to a min-width of 160px. Of course that can be changed.

Bootstrap Dropdown get value of selected item

With the Bootstrap dropdown-toggle buttons is there any way to show the value that has just been selected?
<form action="/output/" name="InputCacheCheck" method="post">
{% csrf_token %}
<div class="input-prepend input-append">
<div class="btn-group">
<button class="btn dropdown-toggle" name="recordinput" data-toggle="dropdown">
Record
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li>A</li>
<li>CNAME</li>
<li>MX</li>
<li>PTR</li>
</ul>
<input class=".input-large" name="hostnameinput" id="appendedInputButton" type="text">
<button class="btn" type="submit">Lookup</button>
</div>
</div>
</form>
Also the name recordinput isn't being supplied within the forms POST data.
Any ideas?
You should be able to change the dropdown text like this..
$(".dropdown-menu li a").click(function(){
var selText = $(this).text();
$(this).parents('.btn-group').find('.dropdown-toggle').html(selText+' <span class="caret"></span>');
});
Demo http://www.bootply.com/64302
How is the form being 'POST'ed?
Or you can do it likes this...
Using (documemt).on seems to work more consistently than targeting the click event from the element class. Also, by giving the button element an ID helps with assigning your dropdown selection in this case.
$(document).on('click', '.dropdown-menu li a', function () {
var selText = $(this).text();
$('#dLabel').html(selText + '<span class="caret"</span>');
});
<div class="dropdown">
<button type="button" class="btn btn-primary dropdown-toggle" id="dLabel" role="button" data-toggle="dropdown" data-target="#">
Lists
<span class="caret"></span>
</button>
<ul id="drpdn_List" runat="server" class="dropdown-menu" role="menu" aria-labelledby="dropdownmenu">
</ul>
</div>
In this case you should not use Bootstrap dropdown toggle. This is useful to show one or more hyperlinks when user click / hover on a button.
if you want style dropdown, you can use some other plugins like selectfx which acts/returns value like a dropdown.