I have two select option fields--one with the ID of determined and the other vehicle_type. Determined has 4 options--2 of which should disable the options in the vehicle_type select field. In the console, I see the disabled attribute being added, but because of Livewire, is immediately removed. Any way of persisting my changes and force Livewire not to send updates to the server?
<select wire:model="determined" id="determined"
name="determined">
<option selected value>Please Select</option>
<option value="0">Published HP Figure (DIN)</option>
<option value="1">Measured with Dynojet+Dyno</option>
<option value="2">Measured with Mustang Dyno</option>
<option value="3">Measured with Engine Dynamometer Cell</option>
</select>
<select wire:model="vehicle_type" id="vehicle_type"
name="vehicle_type">
<option selected value>Please Select</option>
<option value="0">Stick shift and 2WD vehicle</option>
<option value="1">Automatic or 4WD Drive</option>
</select>
#push('scripts')
$(document).ready(function() {
$('#determined').on("change", function() {
const dis = $(this).val() == 0 || $(this).val() == 3;
$("#vehicle_type option").prop("disabled",dis)
});
});
#endpush
Use .defer on the wire:model to prevent Livewire from making an ajax request immediately and re-render the component.
<select wire:model.defer="determined" id="determined" name="determined">
<option selected value>Please Select</option>
<option value="0">Published HP Figure (DIN)</option>
<option value="1">Measured with Dynojet+Dyno</option>
<option value="2">Measured with Mustang Dyno</option>
<option value="3">Measured with Engine Dynamometer Cell</option>
</select>
Related
Am new to Aurelia Js , here, am using a simple select box but its not working while changing the value.
HTML :
<select value.bind="selectVal" change.delegate="changed()">
<option value="" disabled selected>Doc.Type</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
Model :
this.selectVal = '';
changed(){
alert();
}
change.delegate is not triggering while change the value. Also, datepicker value is not loading in the model. What will be the issue? Is it any from my end or form Aurelia js issue.
When using Materialize (as became apparent from the comments of the original questions) the select element won't fire any change event. You would have to listen to the jQuery change event and fire a CustomEvent in the event handler.
Like this:
_suspendUpdate = false;
attached() {
$(this.option).material_select()
$(this.option).on('change', e => {
if (!this._suspendUpdate) {
let customEvent = new CustomEvent('change', {});
this._suspendUpdate = true;
this.option.dispatchEvent(customEvent);
this._suspendUpdate = false;
}
});
}
Note: the suspendUpdate "trick" is needed because a change CustomEvent also causes jQuery to fire its own change event which causes an infinite loop.
The view template for the snippet above:
<template>
<require from="materialize/dist/css/materialize.css"></require>
<div class="input-field col s12">
<select ref="option" value.bind="optionSelect">
<option value="" disabled selected>Choose your option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<p>Selected value: ${optionSelect}</p>
</div>
</template>
Here is also a gist.run which demonstrates this approach:
https://gist.run/?id=4e7dd11228407e765844570409d210bd
Of course if you're using Materialize with Aurelia, you can also use the Materialize bridge: http://aurelia-ui-toolkits.github.io/demo-materialize/#/about
Disclaimer: I'm one of the creators of Aurelia Materialize bridge.
I have form with input select in the aplication/view codeigniter
and the form is submited by codeigniter, no ajax.
<select name="sel_options">
<option value="1">hi</option>
<option value="2" selected>bye</option>
</select>
in my aplication/controller
$this->input->post("sel_options");
result:
2
but i need the text ("bye");
When you post a value, it's going to use the value from the input. So in your case:
<select name="sel_options">
<option value="1">hi</option>
<option value="<!-- I'm Submitting this value -->" selected>bye</option>
</select>
The value for the second option is 2 (<option value="2" ... />) if you wanted to pull in the value from the html ('bye'), you'd have to set the value to be bye.
<option value="bye" selected>bye</option>
I have three dropdowns with same class name:
<select class="MyClass">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<select class="MyClass">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<select class="MyClass">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
I want to change any of these dropdown values based on any other dropdrop selected. If I select option two from second dropdown - I want 1st and 3rd dropdown values to be two. All these dropdown should change values no matter which one I select.
Thanks.
You will need JavaScript to do this. Here is an code example using jQuery:
$('.MyClass').on('change',function(){
$('.MyClass').val( $(this).val() );
});
This will add an EventListeneer for the change Element and update all DropDowns to the value of the changed one.
http://jsfiddle.net/CWP7Q/
you can use jQuery or normal JS, but my example contains Jquery, which will give you an idea. Please check the example in Jsfiddle.
The example binds a change event to the class name of the three selectors:
$(".MyClass").change(function(){
$(".MyClass").val($(this).val());
});
and Voila, every selector gets changed.
Somehting like (with jQuery)
$('.MyClass').change(function() {
$('.MyClass').val($(this).val());
});
I have a select list say of states in the country, which i have in a helper to include easily in any form. (removing most options to make it brief).
I have the value of the current selection stored in the database say "CA". How would i set selected="true" to option CA before rendering the list to the user?
#helper StateSelect(string name = "State")
{
<select name="#name" id="#name" class="required">
<option value="">-- Select -- </option>
<option value="AK">Alaska</option>
<option value="AL">Alabama</option>
<option value="AR">Arkansas</option>
<option value="AZ">Arizona</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="VA">Virginia</option>
<option value="VT">Vermont</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
}
As Darin Dimitrov says, the built-in stuff would be better. However, if you do need to, I think you have a few options:
Add code like this to every line:
<option value="CA" #(name == "CT" ? "selected=selected" : "")> Connecticut</option>
Just re-add the selected item to the top of the list
This keeps the code cleaner, the selected option is just repeated at the top (and selected there), before the entire list
<form method="post">
<select name="users" id="users" onchange="showUser(this.value)" value="">
<option value="">Select a person:</option>
<option value="1" onchange="copy();">tcs</option>
<option value="2" onchange="copy();">wipro</option>
<option value="3" onchange="copy();">Hcl</option>
<option value="4" onchange="copy();">krystal kones</option>
</select>
</form>
I have this drop down i want that when the value is changed like tcs,wipro or hcl then that value should shown in html label
Try putting the onChange attribute in the select tag.
Example: http://jsfiddle.net/r6Fus/
HTML:
<div id="label"></div>
<select id="mySelect" onchange="copy();">
<option value="">Select a person:</option>
<option value="tcs" >tcs</option>
<option value="wipro" >wipro</option>
<option value="Hcl" >Hcl</option>
<option value="krystal kones" >krystal kones</option>
</select>
Javascript:
function copy() {
document.getElementById("label").innerHTML = document.getElementById("mySelect").value
}
Otherwise you could use jQuery.
First of all, the onchange event is for the select element, not the option elements. Those don't actually change. Also, you have two JavaScript functions. showUser() and copy(). But you describe only one piece of functionality. What do these two functions do?
As for showing the text in the label, here's one way to do it (using jQuery, because everybody does):
$(document).ready(function() {
$('#users').change(function() {
$('#myLabel').text($(this).val());
});
});
What this is basically doing is:
Wait until the DOM is loaded and ready.
Bind a function to the change event of the specified select element.
The function contains one line, which sets the text of the specified label to the value of the select.