ChatGPT解决这个技术问题 Extra ChatGPT

How to submit form on change of dropdown list?

I am creating a page in JSP where I have a dropdown list and once the user selects a value he has to click on the go button and then the value is sent to the Servlet.

            </select>
            <input type="submit" name="GO" value="Go"/>

How do I make it so that it does it on change? E.g. when the user selects John all his details are retrived from the DB and displayed. I want the system to do it without having to click the go button.


B
BalusC

Just ask assistance of JavaScript.

<select onchange="this.form.submit()">
    ...
</select>

See also:

HTML dog - JavaScript tutorial


A lot of users block javascript, is there a way to do this without 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.
@deweydb "A lot of users block javascript…" Citation? I've honestly never come across a user that blocks javascript - I've started to believe that these users are a myth. It would prevent them from using half of the internet, for one thing; and anyone savvy enough to block script is probably aware of how innocuous and ubiquitous it is.
@BalusC please add this for people that are worried about users not having javascript enabled <noscript>This form requires that you have javascript enabled to work properly please enable javascript in your browser.</noscript> also @NathanHornby if you really want a citation why dont you go to a public library most public library's block javascript for security reasons as well as some schools too so don't try to pass it off as a myth it has and is being done mainly for security reasons to keep the integrity of computers as some javascript is malicious code...
@deweydb It's simple. Just put a submit button in <noscript> tags. It will only show if the users use noscript. Users who have JS enabled can simple select the element, and users with noscript will just click the submit button. :)
M
MD Sayem Ahmed

Simple JavaScript will do -

<form action="myservlet.do" method="POST">
    <select name="myselect" id="myselect" onchange="this.form.submit()">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
        <option value="4">Four</option>
    </select>
</form>

Here is a link for a good javascript tutorial.


Thanks!, Above one is worked for MVC too @using (Html.BeginForm("Actioname", "controllername", FormMethod.Post))
s
sate wedos

other than using this.form.submit() you also submiting by id or name. example i have form like this : <form action="" name="PostName" id="IdName">

By Name :


No JavaScript needed!
I know, that's what I meant by my comment. I was trying to give you some props. :-)
Wait, is that seriously not JavaScript?
Anything written inside on* attributes is interpretted as Javascript. So PostName.submit() is javascript.
The parentheses indicate we're dealing with a "function". HTML is a declarative and not an imperative language, so it doesn't have the concept of a function so submit() is JavaScript. Your other cue would be when you write an tag for example, you are telling the browser what exists there. When you write PostName.submit() you are telling the browser what to do under a certain condition. That's roughly the difference between declarative and imperative languages.
b
beaver

To those in the answer above. It's definitely JavaScript. It's just inline.

BTW the jQuery equivalent if you want to apply to all selects:

$('form select').on('change', function(){
    $(this).closest('form').submit();
});

Simpler methods exist. Keep it simple.