You can check whether in the string inside your option
which has been selected has the word Devendo
, it shows the Alert.
Using jQuery is an option:
$('select').on('change', function(){
if( $(this).find('option:selected').text().indexOf('Devendo') > 0) {
alert('O Cliente esta devendo!');
}
});
If you don’t want to use jQuery, below is only in Javascript:
var element = document.getElementById('cliente');
element.addEventListener("change", myFunction);
function myFunction() {
var curSelected = this.querySelectorAll('option:checked')[0];
if(curSelected && curSelected.innerHTML.indexOf('Devendo') > 0){
alert ("O Cliente está devendo!");
}
}
You can check working as below:
/* utilizando jQuery */
$('select').on('change', function(){
if( $(this).find('option:selected').text().indexOf('Devendo') > 0) {
alert('O Cliente esta devendo pelo jQuery!');
}
});
/* utilizando apenas Javascript */
var element = document.getElementById('cliente');
element.addEventListener("change", myFunction);
function myFunction() {
var curSelected = this.querySelectorAll('option:checked')[0];
if(curSelected && curSelected.innerHTML.indexOf('Devendo') > 0){
alert ("O Cliente está devendo pelo Javascript!");
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="js-example-basic-single form-control" id="cliente" name="cliente">
<option value="Selecione o Cliente"> Selecione o Cliente</option>
<option value="Selecione o Cliente2"> Selecione o Cliente 2 Devendo</option>
<option value="Selecione o Cliente3"> Selecione Devendoo Cliente</option>
<option value="Selecione o Cliente4"> Selecione o Cliente</option>
</select>
The OP commented that he was using jQuery? If not, it’s foolhardy to assume that he would include the library just for the sake of a
alert()
...– Wtrmute
This is an option ... he also said that he could not use jQuery... qq way to do the same way using only Javascript...
– andrepaulo
I tried here it didn’t work
– wribeiro
@wribeiro posta o resultado final do seu html... por favor - sem o PHP
– andrepaulo
Instead of giving a downvote in response, it is better to ask for OP improve the question. Why for a
alert
just do:<script type="text/javascript"> var situacao = "<?= $dados['situacao'] ?>"; if(situacao == "Devendo"){ alert("Devendo!");} </script>
– Not The Real Hemingway
People using jquery got, but gave another problem, Alert shows 3 or 4 times in a row, as if you had in the loop too
– wribeiro
It’s actually in the loop... do you need Alert when you load? Or when you click on the options you have?
– andrepaulo
@andrepaulo it should show the Alert, when I click on the client should, and currently it is happening this, if you click on a client ok, it does not show the Alert, in the should occurs 2,3x does not have a certain number
– wribeiro
You came to do as I proposed in the reply?
– andrepaulo
@andrepaulo used the following $('select'). on('change', Function(){ if( $(this). find('option:Selected'). text(). indexof('Should') > 0) { Alert('The Customer is owed by jQuery! '); } });
– wribeiro
You put this at the bottom of your page?
– andrepaulo
@andrepaulo, exact created a function, and in select I call her with onchange
– wribeiro
I think I have identified here, what happens is the following, when selecting the first time the client 'should' it shows 1x the Alert, if I click again on another it adds +1, then it shows 2x and so on, always adding 1
– wribeiro
There’s the point... you don’t need to create function and you don’t need to call on the onchange.. This is already being done... just put the code at the bottom of the page... it will do just that for you.. I’ll explain better in the answer..
– andrepaulo
ah, got it right now
– wribeiro
good! I’m glad the answer helped you
– andrepaulo