I am using the JQuery UI Autocomplete. I needed it to MustMatch so I set it up like this:
$('#MyAutoComplete').autocomplete({
source: result,
minLength: 1,
change: function (event, ui) {
if (!ui.item) {
$('#MyAutoComplete').val('');
}
}
});
Don't worry about the result as the field is populated and is working as expected. When someone types in a value not in the list and clicks off the field, the field gets erased as I wished--except when I click a submit button. A simplified version of my submit handler is:
$('#MyButton').get(0).onclick = $('#MyForm').get(0).onsubmit = (function () {
$('#MyForm').validate();
var isValid = $('#MyForm').valid();
if (!isValid) {
return false;
}
});
If I click directly from the focused AutoComplete with bad data to the submit button, the submit event happens before the change for the AutoComplete. So the bad data gets submitted. Any other action clears the bad data which causes the submit to fail (because the field is required).
Any ideas how to get around this?
I have tried adding each of these to the top of the submit event. None made a difference.
$('#MyAutoComeplete').data('autocomplete')._trigger('change');
$('#MyAutoComplete').blur();
$('#MyButton').focus();
Does the combobox feature require a higher version of the script then I am using? If I take this:
$('#MyAutoComplete').autocomplete({
source: result,
minLength: 1,
change: function (event, ui) {
if (!ui.item) {
$('#MyAutoComplete').val('');
}
}
});
And turn it into this:
$('#MyAutoComplete').combobox({
source: result,
minLength: 1,
change: function (event, ui) {
if (!ui.item) {
$('#MyAutoComplete').val('');
}
}
});
I get this error:
JavaScript runtime error: Object doesn't support property or method 'combobox'
I am using:
jQuery UI 1.8.7
If it must be in the list, you should use the combobox functionality. The basic syntax is:
<select id="combobox">
<option value="">Select one...</option>
<option value="ActionScript">ActionScript</option>
<option value="AppleScript">AppleScript</option>
<option value="Asp">Asp</option>
<option value="BASIC">BASIC</option>
<option value="C">C</option>
<option value="C++">C++</option>
<option value="Clojure">Clojure</option>
<option value="COBOL">COBOL</option>
<option value="ColdFusion">ColdFusion</option>
<option value="Erlang">Erlang</option>
<option value="Fortran">Fortran</option>
<option value="Groovy">Groovy</option>
<option value="Haskell">Haskell</option>
<option value="Java">Java</option>
<option value="JavaScript">JavaScript</option>
<option value="Lisp">Lisp</option>
<option value="Perl">Perl</option>
<option value="PHP">PHP</option>
<option value="Python">Python</option>
<option value="Ruby">Ruby</option>
<option value="Scala">Scala</option>
</select>
<script>
$(function() {
$( "#combobox" ).combobox();
});
</script>
You can see more on how to use it at http://jqueryui.com/autocomplete/#combobox
Related
I have this select2 where
If first option is selected, all other options should be disabled.
If any other option is selected, first option should be disabled.
Any help?
<select class="select2">
<option value="Package">Complete Package</option>
<option value="Hotel">Hotel Only</option>
<option value="Transport">Transport Only</option>
<option value="Misc">Misc</option>
</select>
Library select2
https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js
https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css
**reference fiddle** : https://jsfiddle.net/bindrid/hpkqxto6/
<select multiple id="example" style="width: 300px">
<option value="Package">Complete Package</option>
<option value="Hotel">Hotel Only</option>
<option value="Transport">Transport Only</option>
<option value="Misc">Misc</option>
</select>
$(function() {
$('#example').select2();
//Select2 Event handler for selecting an item
$('#example').on("select2:selecting", function(evt, f, g) {
disableSel2(evt, this, true);
});
// Select2 Event handler for unselecting an item
$('#example').on("select2:unselecting", function(evt) {
disableSel2(evt, this, false);
});
});
// At some point during the select2 instantation it created the
// data object it needs with the source select option.
// This function, called by the events above to set the current status for the
// group for which the selected option belongs.
function disableSel2(evt, target, disabled) {
// Found a note in the Select2 formums on how to get the item to be selected
var selId = evt.params.args.data.id;
var aaList = $("option", target);
$.each(aaList, function(idx, item) {
var data = $(item).data("data");
if (selId == "Package") {
if(data.id != selId){
data.disabled = disabled;
$('#example > option').prop("selected", false);
}
}
})
}
I have an HTML.DropDownList where I can set datavaluefield and datatextfield like the following,
<select>
<option value="Fruit">APPLE</option>
</select>
Even, I can able to set the ID to dropdown list using html attributes but Since I need to set the 'Id' to the option like following,
<select>
<option ID='1' value="Fruit">APPLE</option>
</select>
Can anyone share the idea on it...
You can use the id attribute to set the ID to the option tag and use this.value into the onchange callback with select element.
Below is the working example
document.getElementById("select1").onchange = function() {
if (this.value == 'Fruit') {
var optionID=document.getElementById('1');
alert(optionID.value);
}
if (this.value == 'Flower') {
var optionID=document.getElementById('2');
alert(optionID.value);
}
}
<select name="select01" id="select1" onchange="handleSelect()">
<option value="Fruit" id="1">Apple</option>
<option value="Flower" id="2">Rose</option>
</select>
Try this, It will alert your selected value
<select onchange="alert(this.value)">
<option id="1">APPLE</option>
<option id="2">Orange</option>
</select>
I am using #Html.DropDownListFor to build a select object that looks like this:
<select id="GroupCode" name="GroupCode" tabindex="4"><option value="">Select One</option>
<option value="1">One thing</option>
<option value="17">Another thing</option>
<option value="7">A Third thing</option>
</select>
There comes a time when something else changes on the page, and I want to swap out the options, ending up with something like
<select id="GroupCode" name="GroupCode" tabindex="4"><option value="">Select One</option>
<option value="21">A completely different list</option>
<option value="17">A second item only</option>
</select>
However, when I set a breakpoint in the view and look at $('#GroupCode').html, I see something like this:
$('#GroupCode').html
function(a){return p.access(this,function(a){var c=this[0]||{},d=0,e=this.length;if(a===b)return c.nodeType===1?c.innerHTML.replace(bm,""):b;if(typeof a=="string"&&!bs.test(a)&&(p.support.htmlSerialize||!bu.test(a))&&(p.support.leadingWhitespace||!bn.test(
__proto__:
function() {
[native code]
}
arguments: null
caller: null
length: 1
prototype: {...}
where I was expecting to see something like the html above.
What I'm concerned about is whether, by replacing the html for #GroupCode, I will lose what was provided by the #Html.DropDownFor code.
Bottom line: what is a good way to replace the contents of a dropdown list like this?
See this Working fiddle example
HTML
<select id="GroupCode" name="GroupCode" tabindex="4">
<option value="">Select One</option>
<option value="1">One thing</option>
<option value="17">Another thing</option>
<option value="7">A Third thing</option>
</select>
<button id="testBtn" type="button">replace list with items with ids (21, 7)</button>
Script:
$(function () {
$(document).on("click", "#testBtn", function () {
$('#GroupCode').empty().append(
$('<option/>', {
value: "",
text: 'Select One'
}),
$('<option/>', {
value: "21",
text: "A completely different list"
}),
$('<option/>', {
value: "17",
text: "A second item only"
}));
});
});
I have a select box which contains the options and optgroup that are generated dynamically using php.Now when I select "ALL" all the other optgroup, options should be disabled and when I select any option other than "ALL" the "ALL" option should be disabled
<select name="select1" id ="select1" onchange="handleSelect()">
<option value ="-1">ALL</option>
<optgroup label="CARS">
<option value="ford">FORD</option>
<option value="Nissan">Nissan</option>
</optgroup>
</select>
<script>
function handleSelect() {
var selectVal = $("#select1:selected").text();
if (selectVal == "ALL") {
// cannot disable all the options in select box
$("#select1 option").attr("disabled", "disabled");
}
else {
$("#select1 option[value='-1']").attr('disabled', 'disabled');
$("#select1 option").attr('disabled', '');
}
}
</script>
How can I make this working?
This is kind of a strange thing to be doing but here's code that meets your requirements.
$('select').on('change', function() {
if (this.value == '-1') {
$('optgroup option').prop('disabled', true);
} else {
$('optgroup option').prop('disabled', false);
}
});
Live Example - http://jsfiddle.net/NpNFh/
You can use the following code.Let us consider you have three select boxes as follows
<select class="sel_box" id="sel_1" onchange="disable_sel(this.value);" ></select>
<select class="sel_box" id="sel_2" onchange="disable_sel(this.value);" ></select>
<select class="sel_box" id="sel_3" onchange="disable_sel(this.value);" ></select>
and in function let 'opt' is argument now use following
$('.sel_box option[value="'+opt+'"]').attr("disabled", true);
You can use two variations on the syntax for the disabled attribute, depending on the version of HTML you are targeting:
HTML4: <option value="spider" disabled>Spider</option>
XHTML: <option value="spider" disabled="disabled">Spider</option>
<select name="select-choice-1" id="select-choice-1" data-native-menu = false multiple=true>
<option value="standard">Standard: 7 day</option>
<option value="rush">Rush: 3 days</option>
</select>
$("#select-choice-1").selectmenu({
change: function() {
DataObj.GetJson(DataObj.GetRequestUrl("InitData","sessionKey="+ DataObj.sessionKey),"InitData");
}
});
That's all my code about select change above.But It doesnt work when I changed the selected menu.I got no idea how it happened
Try this:
$("#select-choice-1").selectmenu();
$("#select-choice-1").bind( "change", function(event, ui) {
DataObj.GetJson(DataObj.GetRequestUrl("InitData","sessionKey="+ DataObj.sessionKey),"InitData");
});