简体   繁体   English

更改所选选项后提交下拉表单

[英]Submit dropdown form upon changing selected option

So I have a dropdown menu with 'Next' and 'Prev' (previous) buttons. 因此,我有一个带有“下一个”和“上一个”(上一个)按钮的下拉菜单。 I would like it so upon pressing next or prev it would also submit the dropdown choice. 我希望如此,因此在按下next或prev时,它还将提交下拉选项。

<input type="button" id="change2" value="prev" />
<select id="Questions">
  <option value="QA">QUESTION A</option>
  <option value="QB">QUESTION B</option>
  <option value="QC">QUESTION C</option>
  <option value="QD">QUESTION D</option>
</select>
<input type="button" id="change" value="next" />

And here is the JQuery for it: 这是它的JQuery:

$(window).load(function(){
    var dd = $('#Questions'),
        max_len = dd.find('option').length;

    $('#change').click(function () {
        var x = dd.find('option:selected').index();
        if (max_len == x + 1) {
            x = -1;
        }
        dd.find('option').eq(x + 1).prop('selected', true);
    });
    $('#change2').click(function () {
        var x = dd.find('option:selected').index();
            if (max_len == x + 1) {
                x = -1;
            }
        dd.find('option').eq(x - 1).prop('selected', true);
    })
});

How can I make it so that when I click next or prev it submits the form too? 我该如何做,以便当我单击“下一步”或“上一步”时,它也提交表单?

Thanks 谢谢

You could use $.ajax(); 您可以使用$.ajax(); .

A very general solution might look something like this: 一个非常通用的解决方案可能看起来像这样:

$(document).ready(function() {
    $("#change, #change2").on('change', function(){
        $.ajax({
            url: 'sendTheDataHere',
            type: 'POST',
            data:  {keyname:$('#Questions option:selected').val()},
            success: function () {},
            error: function () {}
        });
    });
});

You can submit the form using jQuery. 您可以使用jQuery提交表单。 Check the top answer on this question for details. 查看此问题的顶部答案以获取详细信息。 Assuming you have an action attribute set on the form, something like this might be all you need: 假设您在表单上设置了一个action属性,则可能只需要这样的内容:

$('#change').click(function () {
    // your other logic here
    $('form').submit();
});
<input type="button" id="change2" value="prev" onclick="changeS()"/>
<select id="Questions">
  <option value="QA">QUESTION A</option>
  <option value="QB">QUESTION B</option>
  <option value="QC">QUESTION C</option>
  <option value="QD">QUESTION D</option>
</select>
<input type="button" id="change" value="next" onclick="changeS()"/>

JS Code JS代码

<script>
function changeS() {
/* document.getElementById('change').getElementsByTagName('option')[1].selected = true;*/
var r=document.getElementById('change').selectedIndex+1;
document.getElementById('change').getElementsByTagName('option')[r].selected = true;
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM