0

我想根据选择的单选按钮显示/隐藏列表项。我有 3 个按钮,每个按钮都应该显示一组不同的字段(在列表项内)。出于某种原因,只有最后一个有效。我究竟做错了什么?我遵循了这里的建议,并据我所知进行了比赛。

jsfiddle 在这里发布:http: //jsfiddle.net/Vm2aA/

这是我的 jQuery:

        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
        <script type="text/javascript">

                    $(document).ready(function() {
                        $( "#datepicker" ).datepicker({ minDate: -1, maxDate: "+24D" });
                        $('#pancettaForm').change(function() {
                            if ($('#ship-address').prop('checked')) {
                                $('#address').show();
                            } else {
                                $('#address').hide();
                            }
                            if ($('#ship-date').prop('checked')) {
                                $('#new-ship-date').show();
                            } else {
                                $('#new-ship-date').hide();
                            }    
                            if ($('#ship-both').prop('checked')) {
                                $('#address, #new-ship-date').show();
                            } else {
                                $('#address, #new-ship-date').hide();
                            }        
                        });
                    });
        </script> 

html

    <form name="pancettaForm" method="post" action="http://lizlantz.com/lcform.php" id="pancettaForm">
            <input type="hidden" value="Pancetta Order Update" name="subject"> 
            <input type="hidden" value="cookware/partners_10151_-1_20002" name="redirect">
            <ul>
                <li>
                <label for="update-ship">I'd like to:</label> 
                    <input id="ship-address" name="update-ship" type="radio" value="update-ship-address"/> Have pancetta shipped to a different address than my skillet<br />
                    <input id="ship-date" name="update-ship" type="radio" value="update-ship-date" /> Have pancetta shipped sooner than June 14, 2013 <br />
                    <input id="ship-both" name="update-ship" type="radio" value="update-both" /> Make changes to both the shipping address and shipping date
                </li>
                <li>                
                <label for="order-number"><em>*</em>Order Number (available in order confirmation email):</label> 
                    <input type="text" name="order-number">
                </li>             
                <li>                
                <label for="full-name"><em>*</em>Recipient Full Name:</label> 
                    <input type="text" name="full-name">
                </li>   
                <li id="address" style="display: none;">
                    <label for="address">
                        <em>*</em>Address
                    </label> 
                    <input type="text" name="address">
                    <label for="address2">
                        Address Line 2
                    </label> 
                    <input type="text" name="address2">
                </li>
                <li id="address2" style="display: none;">
                    <label for="city">
                        <em>*</em>City
                    </label> 
                    <input type="text" name="city">
                    <label for="state">
                        <em>*</em>State
                    </label> 
                    <select name="state">
                        <option value="AL">Alabama</option>
                        <option value="AK">Alaska</option>
                        <option value="AZ">Arizona</option>
                    </select>
                    <label for="zip">
                        <em>*</em>Zip Code
                    </label> 
                    <input type="text" name="zip">
                </li>
                <li id="new-ship-date"  style="display: none;">
                    <em>*</em><label for="update-ship">New Ship Date:</label>
                    <input type="text" id="datepicker" />
                </li>            
                <li>
                    <label for="phone">
                        <em>*</em>Phone (for delivery quetsions)
                    </label> 
                    <input type="text" name="phone">
                </li>               
            </ul>
                   <input type="submit" id="button" name="submit" class="green">

          </form>
4

2 回答 2

1

如果您只想针对单选按钮而不是有选择地隐藏和显示项目,我建议您遵循“全部隐藏然后显示特定列表”的方法。它会让你摆脱所有的else{hide...}条款,并使你的代码更易于维护。试试下面的代码

        $('#pancettaForm').change(function () {

           //hide all optional li's
           $('#address,#address2,#new-ship-date').hide();

           //and then show which one is necessary
           if ($('#ship-address').prop('checked')) {
              $('#address').show();
           }

           else if ($('#ship-date').prop('checked')) {
              $('#new-ship-date').show();
           }

           else if ($('#ship-both').prop('checked')) {
              $('#address, #new-ship-date').show();
           }
        });

还要注意else if而不是if。该代码也适用,因为在给定实例else if中只能检查一个单选按钮。请注意,我已将选择器更改为仅与您的单选按钮一起使用

更新

上面的代码现在已经完成了它的工作,但是您必须注意,这$('pancettaForm').change(...)意味着如果表单以任何方式发生更改(例如,如果文本框值被更新),它将运行。它现在只会导致一些额外的处理。要消除此潜在问题,您可以将上面的代码替换为以下代码

       $('#pancettaForm').change(function (ev) {

           //execute this block only if one of the radio button from "update-ship" group has changed
           if (ev.target.name == 'update-ship') {

              //hide all optional li's
              $('#address,#address2,#new-ship-date').hide();

              //and then show which one is necessary
              if ($('#ship-address').prop('checked')) {
                 $('#address').show();
              }

              else if ($('#ship-date').prop('checked')) {
                 $('#new-ship-date').show();
              }

              else if ($('#ship-both').prop('checked')) {
                 $('#address, #new-ship-date').show();
              }
           }
        }); 
于 2013-05-01T17:56:45.767 回答
0

这是因为您将元素隐藏在最后一个 if 语句中。

 $('#address, #new-ship-date').hide();

这将返回 false (因此在显示其中一个元素之后它再次被隐藏):

if ($('#ship-both').prop('checked'))
于 2013-05-01T17:51:40.067 回答