首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取单选按钮的(条件)值

获取单选按钮的(条件)值
EN

Stack Overflow用户
提问于 2014-03-21 20:30:28
回答 1查看 73关注 0票数 0

我对.JS很陌生,而且我使用的代码不是我最初编写的。我需要得到一些单选按钮的价值,一旦条件选择已经作出。

有条件的部分涉及运输的需要。如果用户需要交通,一组新的单选按钮将显示巴士路线选项。这些是我需要由表单返回的值。

HTML:

代码语言:javascript
复制
<fieldset>
<legend>Transportation Registration</legend>
<div class="col3">
    <div class="checklabel">Transportation needs</div>
    <ul class="checkwrap">
        <li>
            <input name="transportation" type="radio" id="transportation-none" value="None" checked="checked" data-price="0" data-deposit="0" />
            <label for="transportation-none">None</label>
        </li>
        <li>
            <input name="transportation" type="radio" id="transportation-am_only" value="AM Only" data-price="60" data-deposit="50" />
            <label for="transportation-am_only">AM Only</label>
        </li>
        <li>
            <input name="transportation" type="radio" id="transportation-pm_only" value="PM Only" data-price="60" data-deposit="50" />
            <label for="transportation-pm_only">PM Only</label>
        </li>
        <li>
            <input name="transportation" type="radio" id="transportation-round_trip" value="Both AM & PM" data-price="100" data-deposit="50" />
            <label for="transportation-round_trip">Round-Trip</label>
        </li>
    </ul>
</div>
<div align="center"><p><strong>One-Way (AM or PM): $60/week | Round-Trip: $100/week</strong></p></div>                                                 
<div>
    <div class="checklabel">Bus Route 1:</div>
    <ul class="checkwrap">
        <li><input name="busroute" type="radio" id="bus_1" value="Bus 1" class="required" /><label for="bus_1">Bus 1</label></li>
        <li><input name="busroute" type="radio" id="bus_2" value="Bus 2" class="required" /><label for="bus_2">Bus 2</label></li>
        <li><input name="busroute" type="radio" id="bus_3" value="Bus 3" class="required" /><label for="bus_3">Bus 3</label></li>
        <li><input name="busroute" type="radio" id="bus_4" value="Bus 4" class="required" /><label for="bus_4">Bus 4</label></li>
    </ul>
</div>      

与(不完全) .JS:

代码语言:javascript
复制
function transportation() {
if($('#transportation-none').is(':checked')) {
    $('[name=busroute]')
        .removeClass("required")
        .val('')
            .closest('div')
            .hide();
} else {
    $('[name=busroute]')
        .addClass("required")
            .closest('div')
            .show();
}
}

我希望这是合理的。这个页面包含了更多的函数,但是我想我是在调用需要修复的部分。我可能需要提供更多的代码才能得到答案。

EN

回答 1

Stack Overflow用户

发布于 2014-03-22 00:25:04

你已经定义了你的函数,但是,根据你所展示的,你从来没有叫它。可以使用jQuery将此函数调用绑定到传输无线电输入中的任何更改事件:

代码语言:javascript
复制
$('input[name="transportation"]').on('change', transportation);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22568935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档