Retrieving value of a select box populated by an ajax call - php

Here are the two select boxes. I am trying to get the value of C to pass on in a form. Select box C is populated from the ajax call below but I can't seem to get a useable value to pass on to the form. (In its simplest form I am looking for something like var A= 'select box C') then I can send 'A' in the form:
Many thanks in advance people x
<select name="list-select" id="list-select">
<option value="">Please select..</option>
<option value="Chemical">Chemical</option>
<option value="Hardware">Hardware</option>
</select>
<select name="C" id="C"></select>
Here is the jquery bit:
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function ($) {
var list_target_id = 'C'; //first select list ID
var list_select_id = 'list-select'; //second select list ID
var initial_target_html = '<option value="">Please select a colour...</option>'; //Initial prompt for target select
$('#' + list_target_id).html(initial_target_html); //Give the target select the prompt option
$('#' + list_select_id).change(function (e) {
//Grab the chosen value on first select list change
var selectvalue = $(this).val();
//Display 'loading' status in the target select list
$('#' + list_target_id).html('<option value="">Loading...</option>');
if (selectvalue == "") {
//Display initial prompt in target select if blank value selected
$('#' + list_target_id).html(initial_target_html);
} else {
//Make AJAX request, using the selected value as the GET
$.ajax({
url: 'http://www.mypropertyviewing.com/Client_order_form_v1.1/selector.php?svalue=' + selectvalue,
success: function (output) {
$('#' + list_target_id).html(output);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status + " " + thrownError);
}
});
}
});
});
</script>

use
selectvalue=this.value //instead of
selectvalue =$(this).val();

Related

Show and Hide Select Options

I've created a very basic form that has 2 dropdown lists.
Each list contains the same entries:
<option value='1234:0'>Closed</option>
<option value='4567:2'>Open</option>
<option value='6857:1'>Dead</option>
<option value='9856:1'>Alive</option>
<option value='0000:0'>Other</option>
If an entry in dropdown 1 is selected, then that entry should be removed from dropdown 2
If a different entry is selected in dropdown 1, then that should be removed from dropdown 2 and the original entry returned to dropdown 2
if nothing is selected in dropdown 1, then all options should be shown in dropdown 2.
I've created a FIDDLE, showing how far I've got... it's not very far.
Can any one help with this ?
Thanks
Try this
$(function(){
$('#test').change(function () {
var selected = $(this).val();
$("#test2 option").show();
$("#test2 option[value='" + selected + "']").hide();
});
});
If you want to use text you can try this
$(function(){
$('#test').change(function () {
var selected = $('option:selected', this).text();
$("#test2 option").each(function(){
if($(this).text() == selected) {
$(this).hide();
} else {
$(this).show();
}
})
});
});
Since the value of each the options are identical, you can use value attribute to select the match in second select.
$('form').on('change', 'select[name="test"]', function() {
var selected = $(this).val();
$("#test2 option").show();
$("#test2 option[value='" + selected + "']").hide();
});
Demo

jQuery use selected value

I have a select box which is created with PHP like:
echo " <option value=\"".$row['CatId']."\">".$row['CatNaam']."</option>";
Where catid is the id which needs to be sent.
Under the select box I have a jQuery clickable div like:
<div id="addtag" style="cursor: pointer;" onclick="clicktag();">
In my script I have a function clicktag like:
function clicktag()
{
$('#addtag').hide();
alert("Tag added ");
}
</script>
What I want to do is alert the selected value(the catid) so the function must do like:
function clicktag()
{
$('#addtag').hide();
alert("Tag added "+tagid);
}
</script>
But how can I get tagid? I tried a lot of examples but can't get it working.
You need a reference to the <select> element in order to get its value. How you get that kind of depends on how the HTML looks, but the easiest way is to give it an ID (if it doesn't have one already) and select it using that:
<select name="yourSelect" id="yourSelect">
// option tags here
</select>
Then the code:
function clicktag() {
var tagId = document.getElementById('yourSelect').value;
// the rest of your code
}
try val()
function clicktag()
{
$('#addtag').hide();
alert("Tag added "+ $('#yourSelectID').val());
}
Use the change event for the select box:
//where #selectBox is id of your select box
$('#selectBox').change(function() {
var currentValue = $('#selectBox :selected').val();
alert(selectVal);
});
Use .val() to get the <select> element's current value.
Example:
<select name="mySelectElement" id="selectElement">
<option value="0">First element</option>
<option value="1">Second element</option>
...
</select>
<script type="text/javascript">
function onClick() {
alert('The selected value is ' + $('#selectElement').val());
}
</script>
$('#selectBox').live('change',function(){
var currentValue = $('#selectBox :selected').val();
alert(selectVal);
});
The selected value is the catid you say
Put it in a var and it will works.
you can try to put this in your code
function clicktag()
{
$('#addtag').hide();
var currentValue = $('#selectBox :selected').val();
alert(selectVal);
}
</script>

Fill out select box depending on other two select boxes - jquery

I had three drop down menus. Depending on items selected on two lists, the third list should be filled out (ajax post).
This is the html code:
Source Language:
<select name='source_lang' id='source_lang' $dis size='6'>
<option value='en'>EN</option>
<option value='sq'>SQ</option>
.....
</select>
Target Language:
<select name='targ_lang' id='targ_lang' $dis size='6'>
<option value='en'>EN</option>
<option value='sq'>SQ</option>
.....
</select>
Supplier:
<select name='supplier_id' id='supplier_id'>
<option value='1'>Supplier 1</option>
<option value='2'>Supplier 2</option>
</select>
On target and source language change, the supplier select list should be filled out.
Anybody can help me with the jquery? I need to ajax post the source, target language values and as response fill out the supplier select list with the data.
Is this something you're looking for?
$('#targ_lang, #source_lang').change(function(){
$.ajax({
url : '',
method : 'post',
type : 'json',
data : {
select1 : $('#targ_lang').val(),
select2 : $('#source_lang').val()
},
complete : function(result){
var options = $.parseJSON(result);
$('#supplier_id').html("");
for(i=0; i < options.length; i++) {
$('#supplier_id').append(
'<option value="'+ options[i] + '">' + 'Supplier' + options[i] + '</option>'
);
}
});
});
});
On the PHP side you need to send your result like this:
$array = new Array(1,2,3,4); // supplier IDs
echo json_encode($array);
Example using jQuery's .post() method:
$('#targ_lang, #source_lang').change(function(){
$.post("test.php", { source: $('#source_lang').val(), target: $('#targ_lang').val()},
function(data) {
//Change the data of the 3rd select
});
});

Remove drop down entries based on previous selection

I've looked through stackoverflow for an answer and am almost there but need some help.
I have multiple drop down lists with the same options in them. For example three identical lists with the following:
<label for='distlist_1'>Distribution List 1</label>
<select name='distlist_1'>
<option value=''>Please Select:</option>
<option value='1'>All</option>
<option value='2'>All Managers</option>
<option value='3'>Leavers</option>
</select>
The only difference being the name eg, distlist_1, 2, 3 etc. I can add ids if necessary.
When a user selects an option in the first drop down list I need it to be removed from all other drops downs. I found a script that did this.
$('option').click(function(){
$('option:contains(' + $(this).html() +')').not(this).remove();
});
But I need it so that if the user then decides, 'wait I don't need that option in drop down list 1 after all', she picks something else and the option reappears in the other drop downs. The code above removes the options then there is no way of retrieving them until if you click enough they all disappear.
This actually does what you want... This is based on jquery 1.7 and the on() event binder
$(document).ready(function (){
$('select').on('focus', function() {
// on focus save the current selected element so you
// can place it back with all the other dropdowns
var current_value = $(this).val();
// bind the change event, with removes and adds elements
// to the dropdown lists
$(this).one('change.tmp', { v : current_value }, function(e)
{
if ($(this).val() != '')
{ // do not remove the Please select options
$('option[value="' + $(this).val() + '"]')
.not($('option[value="' + $(this).val() + '"]', $(this)))
.remove();
}
if (e.data.v != '')
{ // do not add the Please select options
$('select').not($(this)).append($('option[value="' + e.data.v + '"]', $(this)).clone());
}
});
// on blur remove all the eventhandlers again, this is needed
// else you don't know what the previously selected item was.
$(this).on('blur.tmp', { v : current_value}, function (e)
{
$(this).off('blur.tmp');
$(this).off('change.tmp');
});
});
});
The only thing it doesn't do is ordering everything in the right order. You will have to think of your own way of doing that at the .append function.
I ended up using this as it was concise...
jQuery(document).ready(function() {
var selects = $('.mapping')
selects.change(function() {
var vals = {};
selects.each(function() {
vals[this.value] = true;
}).get();
selects.not(this).children().not(':selected').not(':first-child').each(function() {
this.disabled = vals[this.value];
});
});
});

Combobox jquery find selected val

Hi guys i have a combobox with jquery - but i cant make the second one populated when the first select coming selected alredy. im try to get the value without change the select.
<form>
<select name="tipo" id="Tipo_Id" class="buscaTiposVeiculos">
<option value="1" selected="selected">teste1</option>
<option value="2">teste2</option>
<option value="3">teste3</option>
<option value="4">teste4</option>
</select>
<select name="marca" class="recebeMarcas">
<option value="">--- Select ---</option>
</select>
</form>
jquery
$('select.buscaTiposVeiculos').change(function () {
$("select.recebeMarcas").html('<option value="">Carregando...</option>');
// var opt = $("select.buscaTiposVeiculos"); tried like this
// var val = $(this).val(); tried like this
// var val = $(this).find('option:selected').val(); not works
$('select.recebeMarcas >option').remove();
$.post('/inc/geraCidades.php', {
tipov: $(this).val(),
tipo: "tipo"
}, function (data) {
$('select.recebeMarcas').html('<option value="">Selecione a Marca</option>' + data);
});
});
your selected value is
$("#Tipo_Id option:selected").val();
To trigger change event on page load and populate second select automatically. Some commented code can be removed
/* create the change handler use ID is better selector for efficiancy*/
$('#Tipo_Id').change(function() {
/* this populates second select with one option*/
$("select.recebeMarcas").html('<option value="">Carregando...</option>');
/* this removes option populated in line above*/
$('select.recebeMarcas >option').remove();
$.post('/inc/geraCidades.php', {tipov: $(this).val(), tipo : "tipo"}, function(data) {
$('select.recebeMarcas').html('<option value="">Selecione a Marca</option>'+data);
});
/* trigger the change event on page load*/
}).change();

Categories