0

I'm trying to pass a select value in my ajax function.

my html code:

 <select class="form-control" id="exampleFormControlSelect1" class="selectUser"> <?php foreach($logs as $key => $value){ ?> <option value="<?php echo $value['username'];?>"><?php echo $value['username']; ?></option> <?php } ?> </select> 

and my JS code:

 $('select.selectUser').change(function(){ alert('Select field value has changed to' + $('select.selectUser').val()); $.ajax({ type: 'GET', url: "Panier/loadPanier", data: {username: $('select.selectUser').val()}, success: function(result){ var data1 = JSON.parse(result); alert(data1.username) ; }, }); }); 

but the first alert('Select field value has changed to' + $('select.selectUser').val()); is not generating any alert and I'm not getting any error in my console.

Can you tell me what's wrong?

1
  • Try using just the class $(".selectUser") additionally try checking whether the option tags are actually having a value Commented Jul 7, 2019 at 12:58

2 Answers 2

1

You have added two class attributes to the select element - class="form-control" and class="selectUser".

<select class="form-control" id="exampleFormControlSelect1" class="selectUser"> 

Therefore, it is ignoring your second class - class="selectUser".

$('select.selectUser').on('change', function() { console.log($(this).val()); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select class="form-control selectUser" id="exampleFormControlSelect1"> <option value="1" selected="selected">One</option> <option value="2">Two</option> </select>

Sign up to request clarification or add additional context in comments.

Comments

1

Let's try with $('#exampleFormControlSelect1').on('change', function(){

1 Comment

Could have been a comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.