最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Make <select> options "click" automatically - Stack Overflow

programmeradmin0浏览0评论

Let's say I've got a drop down like this:

<div class="selector">
    <select name="perPage">
        <option value="5">5</option>
        <option value="10">10</option>
        <option value="15">15</option>
        <option value="25">25</option>
    </select>
</div>

I'm know PHP and Javascript (jQuery) so a solution in either would be just fine. But I've got a submit button underneath it, and instead of that, I was wondering if there's a way to click the drop down, pick your value, then have it send that automatically with OUT having to hit a submit?

Let's say I've got a drop down like this:

<div class="selector">
    <select name="perPage">
        <option value="5">5</option>
        <option value="10">10</option>
        <option value="15">15</option>
        <option value="25">25</option>
    </select>
</div>

I'm know PHP and Javascript (jQuery) so a solution in either would be just fine. But I've got a submit button underneath it, and instead of that, I was wondering if there's a way to click the drop down, pick your value, then have it send that automatically with OUT having to hit a submit?

Share Improve this question edited Aug 22, 2012 at 22:23 j08691 208k32 gold badges269 silver badges280 bronze badges asked Aug 22, 2012 at 22:19 XhynkXhynk 13.9k8 gold badges34 silver badges70 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 8

You could use the 'onchange' element of the select:

<select name="dropdown" id="dropdown" onchange="this.form.submit()">

This only uses javascript without the need of loading the whole JQuery library - unless you use JQuery for other functions on the site.

You can listen to the change event using jQuery and submit the parent form automatically. Something like this:

$('.selector select[name=perPage]').on('change', function(e) {
    $(e.currentTarget).closest('form').submit();
});

Assign an id to to select, e.g. selectID, then add jQuery of the form:

$('#selectID').change(function(){
    this.form.submit()
});
$('select[name="perPage"]').on('change', submitForm);

where submitForm is a function that submits the form.

发布评论

评论列表(0)

  1. 暂无评论