2
I’m starting in web development, I have the logic of how to do things, but the problem is implementation. I have a "UL" being populated by database, I would like to know how I capture the selected value of "LI", game for a variable and this variable play for PHP, so I can handle a search. It follows codes. PS: HTML, CSS and PHP I know it well, but the problem is JS.
1 - JS handling combo:
$(document).ready(function () {
$(".btn-select").each(function (e) {
var value = $(this).find("ul li.selected").html();
if (value != undefined) {
$(this).find(".btn-select-input").val(value);
$(this).find(".btn-select-value").html(value);
}
});
});
$(document).on('click', '.btn-select', function (e) {
e.preventDefault();
var ul = $(this).find("ul");
if ($(this).hasClass("active")) {
if (ul.find("li").is(e.target)) {
var target = $(e.target);
target.addClass("selected").siblings().removeClass("selected");
var value = target.html();
$(this).find(".btn-select-input").val(value);
$(this).find(".btn-select-value").html(value);
}
ul.hide();
$(this).removeClass("active");
}
else {
$('.btn-select').not(this).each(function () {
$(this).removeClass("active").find("ul").hide();
});
ul.slideDown(300);
$(this).addClass("active");
}
});
$(document).on('click', function (e) {
var target = $(e.target).closest(".btn-select");
if (!target.length) {
$(".btn-select").removeClass("active").find("ul").hide();
}
});
2 - PHP populating combo:
<label for="exampleInputName2">Cargo do Colaborador</label>
<a class="btn btn-default btn-select">
<input type="hidden" class="btn-select-input" id="" name="" value="" />
<span class="btn-select-value">Selecione o cargo (opcional)</span>
<span class='btn-select-arrow glyphicon glyphicon-chevron-down'></span>
<ul>
<?
$sql="SELECT DISTINCT cargo FROM colaborador WHERE (unidade = 2) or (unidade = 1) order by cargo";
$query = mysql_query($sql);
if(mysql_num_rows($query)){
while($res=mysql_fetch_assoc($query)){
$cargo = $res['cargo'];
echo "<li id='$cargo'>$cargo</li>";
}
}
else{
echo "<li>Nenhum Setor cadastrado!</li>";
}
?>
<!--se quiser um item selecionado, só colocar no LI - class="selected"-->
</ul>
</a>
If you’re doing something wrong, please correct it. Thanks for your help!
What would that be
input[type=hidden]
in HTML? Already to store the selected position? And why is this entire code in an elementa
? I saw no logic in it.– Woss
Hello Anderson, to be honest, I used a code ready from the internet, so I’m not sure what this input is for.
– Ítalo Salgado
To be honest, then you’re getting it wrong. Nothing against getting ready-made codes (as long as they are free), but look for what these codes do before using.
– Woss
This element A defines the style of the button, since Bootstrap is used. Thanks for the tip. I’ll try to understand it.
– Ítalo Salgado