Right now I have three different select box sections, they are all states or provinces or territories, anyways...
My form is like this...
Country[]
States/Province[]
I change the default info in the state/province box by selecting different countries before it, such as USA(new york, florida) Canada(Albert, Nova Scotia) Mexico(Yukatan, Northren).
Each section of select boxes Mexico, USA, Canada all have the same name to submit into the database as a state/province. But, I believe its keeping my other selections of the other countries ie.. Select Alabama for USA and then Switch to Canada and pick Alberta, it tries to save both of them, how can I make it where only one will be chosen?
Thank you so much!
oh, here is some code below...
$(document).ready(function(){
$("#country").change(function()
{
$("#usStates").hide();
$("#caStates").hide();
$("#mexStates").hide();
if ($("#country").val() == 'United States'){
$("#usStates").show();
$("#caStates").hide();
$("#mexStates").hide();
$("#state1").val('');
}
if ($("#country").val() == 'Canada'){
$("#caStates").show();
$("#usStates").hide();
$("#mexStates").hide();
$("#state1").val('');
}
if ($("#country").val() == 'Mexico'){
$("#mexStates").show();
$("#caStates").hide();
$("#usStates").hide();
$("#state1").val('');
}
}); });
<div id="caStates" style="display: none;">
<div class="leftform">State / Province:</div>
<div class="rightform"><select class="select" style="width: 150px;" id="state1" name="state" size="1">
<option value=""></option>
<option value="Alberta">Alberta</option>
<option value="New Brunswick">New Brunswick</option>
<option value="Nova Scotia">Nova Scotia</option>
<option value="Prince Edward Island">Prince Edward Island</option>
<option value="British Columbia">British Columbia</option>
<option value="Newfoundland">Newfoundland</option>
<option value="Ontario">Ontario</option>
<option value="Quebec">Quebec</option>
<option value="Manitoba">Manitoba</option>
</select></div><br clear="all"/>
</div>
<div id="mexStates" style="display: none;">
<div class="leftform">State / Province:</div>
<div class="rightform"><select class="select" style="width: 150px;" id="state1" name="state" size="1">
<option value=""></option>
<option value="Baja California">Baja California</option>
<option value="Central Mexico">Central Mexico</option>
<option value="Gulf of Mexico">Gulf of Mexico</option>
<option value="Northern Mexico">Northern Mexico</option>
<option value="Mexico Pacific Coast">Mexico Pacific Coast</option>
<option value="Yucatan">Yucatan</option>
</select></div><br clear="all"/>
</div>
The problem is that you must disable controls that don't want to be submitted.
Replace your script by this one, and everything will'be fixed:
$(document).ready(function(){
$("#country").change(function(){
//We first disable all, so we dont submit data for more than 1 country
$("#usStates, #caStates, #mexStates").hide().find("#state1").attr("disabled", true);
var $divSelectedStates;
if ($("#country").val() == 'United States')
$divSelectedStates = $("#usStates");
if ($("#country").val() == 'Canada')
$divSelectedStates = $("#caStates");
if ($("#country").val() == 'Mexico')
$divSelectedStates = $("#mexStates");
//We now enable only for the selected country
$divSelectedStates.show().find("#state1").attr("disabled", false).val('');
});
});
The main change in this code (besides it's a little shorter now), is that we're disabling non selected selects, so they are not submitted.
And and advice, don't use repeated id's in the same page. NEVER. This was causing a problem, also, but now it's fixed with the code i showed you.
Hope this helps, Cory. Cheers :)
You should just use a single <select id="state" name="state">, and load different set of options into that <select> each time some causes an onchange event on the <select id="country" name="country"> field.
You could load the changes from JSON files, which you can keep already prepared on your server.
Related
I have requirement to get date between particular years so I have put two dropdown “from year” and “to Year” but I want to make validation for that “to dropdown” value must be great than “form dropdown”
Below is my html code
<div class="graph-filter">
<label>From</label>
<select onchange="change_data_year();" id="from_data_year" name="from_data_year" class="graph-select">
<option selected="" value="2016">2016</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
</select>
<label>To</label>
<select id="to_data_year" name="to_data_year" class="graph-select">
<option selected="" value="2016">2016</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
</select>
<input type="submit" value="Submit">
Try like this
function change_data_year(){
$('#to_data_year option').prop('disabled',false);
var from_data_year=$('#from_data_year').val();
var to_data_year=$('#to_data_year').val();
$('#to_data_year option').filter(function() {
return $(this).val() < from_data_year;
}).prop('disabled', true);
}
I hope that is like you need it.
My Multiselect input code is
<?php echo $this->Form->input('Dispensary.role.',array('options'=>$dispensary_users,'class'=>'form-control dispensary_users_dd','label'=>false,'style'=>'width:300px;','empty'=>'Select Users'));
?>
and generated html code :
<select name="data[Dispensary][role][]" class="form-control dispensary_users_dd" style="width: 300px; display: none;" id="DispensaryRole">
<option value="">Select Users</option>
<option value="9">Yashobanta</option>
<option value="80">Yash</option>
<option value="83">Ramesh</option>
</select>
But when I select all users, and print_r($this->data);
returns last selected input.
But I want all.
I forgot to add multiple => true.
I don't know how that thing with $this->Form->input(...); works, because it's not important. You can select only one opinion without multiple attribute.
Example by w3schools.org:
<select name="cars" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
For saving the multiple select value in database, store the array using implode function.
In controller:
if (!empty($this->data)) {
$this->data['Category']['name'] = implode(",",$this->data['Category']['name']);
$this->Category->create();
$this->Category->save($this->data);
}
Hope it's help :)
Im using jquery.validationEngine.js everything is setup and working fine, however I need to make a change.
I have two dropdown boxes.
Is The Vehicle Imported? [yes, no]
What Is The Country Of Origin? [France, Germany, Japan, USA, UK] etc
What I require is that if the car is imported then to show the country of origin, else display nothing.
At the moment I have both fields working on a live form,
<label>Is The Vehicle Imported?</label>
<select class="validate[required] target" name="strLeadData39" id="strLeadData39">
<option selected="selected" value="">Please Select</option>
<option value="No">No</option>
<option value="Yes">Yes</option>
</select>
<label>What Is The Country Of Origin?</label>
<select class="validate[required] target" name="strLeadData40" id="strLeadData40" >
<option selected="selected" value="">Please Select</option>
<option value="Not Imported">Not Imported</option>
<option value="France">France</option>
<option value="Germany">Germany</option>
<option value="Japan">Japan</option>
<option value="Canada">Canada</option>
<option value="USA">USA</option>
<option value="EU-Other">EU-Other</option>
<option value="Other">Other</option>
</select>
How do Ш complete this?
Thanks
So you want to show/hide the country select depending on the value of the imported select?
$('#strLeadData39').change(function(){
if ( $(this).val() == "Yes" ){
$('#strLeadData40').show();
} else {
$('#strLeadData40').hide();
}
});
Here is a fiddle with a basic example
The above code has nothing to do with jQuery validationEngine,just hiding/displaying the element. To make the form validate you will probably have to dynamically remove/add the validate[required] class using jQuery removeClass/addClass, something inside the if/else like:
$('#strLeadData40').removeClass("validate[required]");
I want a form to have multiple drop down selects that change based on the previous selection. This is how I want it to work.
<select id="format">
<option selected="selected" value="NULL">-- Select a product Format --</option>
<option value="PS3">PS3</option>
<option value="Xbox 360">Xbox 360</option>
<option value="Wii U">Wii U</option>
</select>
So if you select PS3 you are then faced with more options for PS3 product type
<select id="ps3-product-type">
<option selected="selected" value="NULL">-- Select a product PS3 product type --</option>
<option value="chargers">chargers</option>
<option value="controllers">controllers</option>
<option value="headsets">headsets</option>
</select>
So then you select headsets for example and you get another set of final options of products
<select id="ps3-headsets">
<option selected="selected" value="NULL">-- Select a PS3 headset --</option>
<option value="product-1">product 1</option>
<option value="product 2">product 2</option>
<option value="product 3">product 3</option>
</select>
How can I do this with jquery or PHP? Please bare in mind that I will also have selects for Xbox 360 and Wii U too.
UPDATE PROGRESS:
http://jsfiddle.net/maximus83/r7MN9/639/
Here is my HTML, I have got the first set of arrays working for selecting product type, But I cant get the 3rd box working, I need 3rd box to say the product, where do I go from here.
<select id="cat">
<option val="PS3">PS3</option>
<option val="Xbox360">Xbox360</option>
<option val="WiiU">WiiU</option>
<option val="Multiformat">Multiformat</option>
</select>
<select id="item">
</select>
<select id="product">
</select>
Here is my script
PS3=new Array('Headsets(PS3)','Controllers(PS3)','Chargers(PS3)','Cables(PS3)');
Xbox360=new Array('Headsets(360)','Chargers(360)');
WiiU=new Array('Controllers(WiiU)','Headsets(WiiU)');
Multiformat=new Array('Headsets(Multi)','Chairs(Multi)','Cables(Multi)');
populateSelect();
$(function() {
$('#cat').change(function(){
populateSelect();
});
});
function populateSelect(){
cat=$('#cat').val();
$('#item').html('');
eval(cat).forEach(function(t) {
$('#item').append('<option val="#item">'+t+'</option>');
});
}
This should do the trick for you. It's a previously answered thread on the exact same topic - how to populate a drop down based on the value in another. It takes advantage jQuery (And I've used it before myself as reference).
I have a form set up which links to a database to show specific products with the requirement chosen, for example:
Category >
Manufacturer >
Paper Size >
Each of these drop downs have different attributes. The client has requested, that once Software is selected from the Category drop down that the paper size and speed drop downs become disabled.
How would I do this?
My code is:
<select id="category1">
<option value="all">All</option>
<option value="printers">Printers</option>
<option value="software">Software</option>
</select>
<label class="minus_margin">Manufacturer: </label>
<select id="manufacturer1">
<option value="canon">Canon</option>
<option value="HP">HP</option>
<option value="epson">Epson</option>
</select>
<label class="minus_margin">Paper Size: </label>
<select id="paper_size1">
<option value="all">A4 & A3</option>
<option value="A4">A4 Only</option>
</select>
<label class="minus_margin">Speed: </label>
<select id="speed1">
<option value="all">All</option>
<option value="20">0-20</option>
<option value="34">21-34</option>
<option value="44">35-44</option>
<option value="54">45-54</option>
<option value="69">55-69</option>
<option value="89">70-89</option>
<option value="90">90+</option>
</select>`
For example, user selects software, paper size and speed drop-downs become disabled.
Thanks
You could use the onChange event with jQuery to detect when the content of the drop-down has changed and just disable the drop down.
Something like:
$('#dropDown').change(function() {
if ($(this).attr("disabled") == false) { $(this).attr("disabled","disabled"); }
}
To Enable it back you would use something like:
$('#dropDown').removeAttr("disabled");
EDIT:
$('#category1').change(function()
{
var selected = $(this).val();
if (selected == 'software')
{
$('#paper_size1').attr("disabled","disabled");
$('#speed1').attr("disabled","disabled");
} else {
if ($('#paper_size1').attr("disabled") == true) { $('#paper_size1').removeAttr("disabled"); }
if ($('#speed1').attr("disabled") == true) { $('#speed1').removeAttr("disabled"); }
}
});