简体   繁体   English

用jQuery提交值

[英]Submitting a value with jQuery

I've been pulling my hair out with this one, although I'm certain the solution is embarrassingly simple! 尽管可以肯定的解决方案非常简单,但我一直在用这种方法拔头发! I have developed a pull-down menu that requires a selection before presenting more choices, based on the initial selection. 我已经开发了一个下拉菜单,需要根据初始选择在显示更多选择之前进行选择。 It works fine. 工作正常。

However, I need to have jQuery submit the value of the option chosen without a submit button present. 但是,我需要让jQuery在没有提交按钮的情况下提交所选选项的值。 So, basically, when a user selects a fruit size, the user is taken to the relevant page in the option value. 因此,基本上,当用户选择水果大小时,会将用户转到选项值中的相关页面。 I cant figure it out! 我想不通! Heres my code: 这是我的代码:

jQuery: jQuery的:

<script type="text/javascript">
$(document).ready(function()
{
    $('#fruit').change(function()
    {
        var val = $('#fruit').val();
        $('.fruitSubSelect').hide();
        if(val)
        {
        $('#fruit'+val).show();
        $('#noFruit').hide();
        }
    });
});
</script>

CSS to hide size select: 隐藏大小的CSS:

<style type="text/css">
.fruitSubSelect {display: none;}
</style>

HTML: HTML:

<form action="nothing">
<select id="fruit">
<option value="">Choose Fruit</option>
<option>Apple</option>
<option>Orange</option>
</select>
<select id="fruitApple" class="fruitSubSelect">
<option value="">Choose Size</option>
<option value="http://www.mysite.com/big-apple.html">Big Apple</option>
<option value="http://www.mysite.com/small-apple.html">Small Apple</option>
</select>
<select id="fruitOrange" class="fruitSubSelect">
<option value="">Choose Size</option>
<option value="http://www.mysite.com/big-orange.html">Big Orange</option>
<option value="http://www.mysite.com/small-orange.html">Small Orange</option>
</select>
<select id="noFruit">
<option value="">Choose A Fruit First</option>
<option value="">Please Select Fruit First</option>
</select>
</form>

Would appreciate any help! 将不胜感激! Thanks. 谢谢。

I think you're looking for something like this: 我认为您正在寻找这样的东西:

$(".fruitSubSelect").change(function(){
   window.location.href = this.value;
});

Example: http://jsfiddle.net/jonathon/bWUnR/ 示例: http//jsfiddle.net/jonathon/bWUnR/

This will get the selected value of the dropdown and set the window location to it (so the page will go to it). 这将获得下拉菜单的选定值并为其设置窗口位置(因此页面将转到该位置)。

the addition of this into your jquery alert's the selected option's URL: 将此添加到您的jquery警报的选定选项的URL中:

$('.fruitSubSelect').change(function(){
    alert($(':selected',$(this)).val());
});

live example: http://jsfiddle.net/274Gv/ 实时示例: http//jsfiddle.net/274Gv/

With dropdowns, do not use .val(). 对于下拉菜单,请勿使用.val()。 :selected is what you're looking for. :selected是您要寻找的。 http://api.jquery.com/selected-selector/ http://api.jquery.com/selected-selector/

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

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