Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I auto submit a dropdown when a value is selected other than the first value?

Current Code:

<select name='PreviousReceiver' onchange='this.form.submit()'>
     <option value='0'>Contact List</option>
     <option value='1'>blah</option>
     <option value='2'>blah2</option>
     <option value='3'>blah3</option>
</select>

So I have this dropdown box that auto submits when the user clicks it. It works fine the way it is, but I dont want it to submit if the user has the first value selected(see above).

In other words, if the user clicks contact list, then I don't want it to submit the form.

I tried this:

 onchange="if(this.value != '0') this.form.submit()" 

but that just made it never activate.

Any suggestions?

like image 684
user1899891 Avatar asked Sep 30 '13 05:09

user1899891


People also ask

How do you pre select a value in the dropdown?

Learn HTML A select box also called drop down box provides an option to list down various options in the form of drop down list. You can also preselect a value in dropdown list of items in HTML forms. For that, add selected in the <option> tag for the value you want to preselect.

Which event is called on selection of an item from a dropdown list?

The HTML Select DropDownList has been assigned a jQuery OnChange event handler. When an item is selected in the HTML Select DropDownList, the jQuery OnChange event handler is executed within which the Text and Value of the selected item is fetched and displayed in JavaScript alert message box.

How do you keep selected value in dropdown?

To retain the selected value in the dropdown on refresh, sessionStorage is used to store the value within the user's browser. First, the values have to be set using sessionStorage. setItem("SelItem", selVal); SelItem is a variable in which we are setting the value of selVal .

How to submit form with select option using JavaScript?

If you have a Html (or Html5, JSP, PHP, ASP) page with a form element containing select option and you want to submit the form once a option is selected from the list of the dropdown then you can make use of javaScript.

Is there an alternative to <select> in drop down list?

A completely different alternative is to use <ul><li><input type="submit"> and throw in a load of CSS to make it look like a true dropdown list. But then there's no means of a <select> anymore. @deweydb "A lot of users block javascript…" Citation?

Is there a way to make a drop down list using JavaScript?

@deweydb: Nope. I would otherwise have answered it :) Just show a <noscript> banner that site's experience is better if JS is enabled. A completely different alternative is to use <ul><li><input type="submit"> and throw in a load of CSS to make it look like a true dropdown list. But then there's no means of a <select> anymore.

How to disable selection of the first item in a form?

function DoSubmit (sel) { if (sel.val ()!='0') this.form.submit (); } maybe you would like something like this? This also disables the selection of the first item This is the same answer as mentioned above,You need to make it more clear or little different.


1 Answers

maybe you would like something like this? This also disables the selection of the first item

<select name='PreviousReceiver' onchange='this.form.submit()'>
  <option selected disabled>Contact List</option>
  <option value='1'>blah</option>
  <option value='2'>blah2</option>
  <option value='3'>blah3</option>
</select>
like image 152
Patrik Avatar answered Oct 28 '22 13:10

Patrik