本文介绍了Jquery在选择选项时提交选择表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的选择:

$ b $我有一个选择列表,里面有很长的列表,我可以用它来搜索它,用select2 jquery plugin。 b

  @using(Html.BeginForm(ProjectList,Client,FormMethod.Get,new {id =clientform,name =clientoverviewform} ))
{
< div class =form-group>
< label for =sel1>选择客户端< / label>
@if(Model.ClientList!= null)
{

@foreach(Model.ClientList中的var客户端)
{
< option value =@ client.Id> @ client.Name< / option>
}

< / select>
}
< / div><! - end form-group - >
< div class =form-group>
< input type =submitvalue =Visclass =form-control>
< / div><! - end form-group - >
}

我需要它可以让我搜索并选择我想要的选项使用。按回车或鼠标点击。然后它提交表单...



和offcourse它必须使用一个正常的提交按钮以及...

我试过这段代码:

  $(document).ready(function(){
$ ('#clientform')。on('change',function(){
var $ form = $(this).closest('form')。 b $ form.find('input [type = submit]')。click();
});
});
});

其中它甚至可以在我搜索之前提交,尽管它可以工作,如果我只是正常滚动列表并且点击..

解决方案

如果您想在更改选择选项后提交表单,请执行以下操作:


'b $ b

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


I have a select, with a long list, in which i can search it, with select2 jquery pluing.

my select:

 @using (Html.BeginForm("ProjectList", "Client", FormMethod.Get, new { id = "clientform", name = "clientoverviewform"}))
        {
            <div class="form-group">
                <label for="sel1">Select Client</label>
                @if (Model.ClientList != null)
                {
                    <select class="form-control selectpicker" data-live-search="true" id="sel1" name="id">

                        @foreach (var client in Model.ClientList)
                        {
                            <option value="@client.Id">@client.Name</option>
                        }

                    </select>
                }
            </div><!-- end form-group-->
            <div class="form-group">
                <input type="submit" value="Vis" class="form-control">
            </div><!-- end form-group-->
        }

I need it to be able to let me search and select the option i want to use. press enter or mouse click. and then it submits the form...

and offcourse it has to work with a normal submit button aswell...

i have tried this code:

$(document).ready(function () {
        $('#clientform').on('change', function () {
            var $form = $(this).closest('form').on('change', function () {
                $form.find('input[type=submit]').click();
            });
        });
    });

in which it submits before i even search, though it works if i just normally scrolls through the list and clicks..

解决方案

If you want to submit the form after you change the select option do:

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

这篇关于Jquery在选择选项时提交选择表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 17:16