【问题标题】:How to show a div after specific inputs are filled out填写特定输入后如何显示div
【发布时间】:2014-03-23 22:20:40
【问题描述】:

这个工作:Here
我想知道如何改变它,以便它可以与表单的特定部分一起使用,而不是全部。

我已经设法让它只使用 ID,而不是类。

到目前为止我已经得到了这个:

            $('.weeks').bind('keyup change',function(){
            // get elements that are empty.
            var empty = $('.weeks').map(function(index, el) {
                return !$(el).val().length ? el : null;
            }).get();

            // could also be placed outside of the function
            var tick = $('#tick3');
            var cross = $('#cross3');

            // check if there are any empty elements, if there are none, show numbers, else hide number.
            !empty.length ? tick.show() : tick.hide();
            !empty.length ? cross.hide() : cross.show();
        });

这是html:

<span class="checked tick" id="tick3"><img src="http://www.clker.com/cliparts/9/d/J/p/D/g/check-grey-md.png" width="38px" style="margin-top: -10px;" /></span>
                    <span class="checked" id="cross3"><img src="http://mypad.northampton.ac.uk/12402247/files/2014/02/red-wrong-cross-hi-23y1kex.png" width="38px" style="margin-top: -10px;" /></span>
<table>
                    <tr class="bottom">
                        <td>
                            <input type="checkbox" id="allWeeks" class="weeks" name="all" onClick="allWeeks(this)"></input>
                            <label for="allWeeks">1-15</label>
                        </td>
                        <td>
                            <input type="checkbox" id="defWeeks" class="weeks" checked name="default" onClick="defaultWeeks(this)"></input>
                            <label for="defWeeks">1-12</label>
                        </td>
                        <td>
                            <input type="checkbox" id="oddWeeks" class="weeks" name="odd" onClick="oddWeeks(this)"></input>
                            <label for="oddWeeks">Odd</label>
                        </td>
                        <td>
                            <input type="checkbox" id="evenWeeks" class="weeks" name="even" onClick="evenWeeks(this)"></input>
                            <label for="evenWeeks">Even</label>
                        </td>
                        <td colspan="2">
                            <button id="clearWeeks">Clear</button>
                        </td>
                    </tr>
                    <tr class="bottom">
                        <td>
                            <input type="checkbox" id="1" class="weeks all_weeks default_weeks odd_weeks" checked name="1" onClick="changed_check();"></input>
                            <label for="1">1</label>
                        </td>
                        <td>
                            <input type="checkbox" id="2" class="weeks all_weeks default_weeks even_weeks" checked name="2" onClick="changed_check();"></input>
                            <label for="2">2</label>
                        </td>
                        <td>
                            <input type="checkbox" id="3" class="weeks all_weeks default_weeks odd_weeks" checked name="3" onClick="changed_check();"></input>
                            <label for="3">3</label>
                        </td>
                        <td>
                            <input type="checkbox" id="4" class="weeks all_weeks default_weeks even_weeks" checked name="4" onClick="changed_check();"></input>
                            <label for="4">4</label>
                        </td>
                        <td>
                            <input type="checkbox" id="5" class="weeks all_weeks default_weeks odd_weeks" checked name="5" onClick="changed_check();"></input>
                            <label for="5">5</label>
                        </td>
                        <td>
                            <input type="checkbox" id="6" class="weeks all_weeks default_weeks even_weeks" checked name="6" onClick="changed_check();"></input>
                            <label for="6">6</label>
                        </td>
                    </tr>
                    <tr class="bottom">
                        <td>
                            <input type="checkbox" id="7" class="weeks all_weeks default_weeks odd_weeks" checked name="7" onClick="changed_check();"></input>
                            <label for="7">7</label>
                        </td>
                        <td>
                            <input type="checkbox" id="8" class="weeks all_weeks default_weeks even_weeks" checked name="8" onClick="changed_check();"></input>
                            <label for="8">8</label>
                        </td>
                        <td>
                            <input type="checkbox" id="9" class="weeks all_weeks default_weeks odd_weeks" checked name="9" onClick="changed_check();"></input>
                            <label for="9">9</label>
                        </td>
                        <td>
                            <input type="checkbox" id="10" class="weeks all_weeks default_weeks even_weeks" checked name="10" onClick="changed_check();"></input>
                            <label for="10">10</label>
                        </td>
                        <td>
                            <input type="checkbox" id="11" class="weeks all_weeks default_weeks odd_weeks" checked name="11" onClick="changed_check();"></input>
                            <label for="11">11</label>
                        </td>
                        <td>
                            <input type="checkbox" id="12" class="weeks all_weeks default_weeks even_weeks" checked name="12" onClick="changed_check();"></input>
                            <label for="12">12</label>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input type="checkbox" id="13" class="weeks all_weeks odd_weeks" name="13" onClick="changed_check();"></input>
                            <label for="13">13</label>
                        </td>
                        <td>
                            <input type="checkbox" id="14" class="weeks all_weeks even_weeks" name="14" onClick="changed_check();"></input>
                            <label for="14">14</label>
                        </td>
                        <td colspan="4">
                            <input type="checkbox" id="15" class="weeks all_weeks odd_weeks" name="15" onClick="changed_check();"></input>
                            <label for="15">15</label>
                        </td>
                    </tr>
                </table>

如果至少有一个带有班级周数的复选框被选中,我希望做的是显示一个勾号,如果没有,则显示一个叉号。

复选框和清除按钮的代码,因此 1-12 检查 1-12 等:

$(document).ready(function () {
            $("#allWeeks").click(function () {
                $(".all_weeks").prop('checked', $(this).prop('checked'));
                $("#defWeeks").attr('checked', false);
                $("#evenWeeks").attr('checked', false);
                $("#oddWeeks").attr('checked', false);
            });

            $("#defWeeks").click(function () {
                $(".all_weeks").attr('checked', false);
                $(".default_weeks").prop('checked', $(this).prop('checked'));
                $("#allWeeks").attr('checked', false);
                $("#evenWeeks").attr('checked', false);
                $("#oddWeeks").attr('checked', false);
            });

            $("#oddWeeks").click(function () {
                $(".all_weeks").attr('checked', false);
                $(".odd_weeks").prop('checked', $(this).prop('checked'));
                $("#allWeeks").attr('checked', false);
                $("#defWeeks").attr('checked', false);
                $("#evenWeeks").attr('checked', false);
            });

            $("#evenWeeks").click(function () {
                $(".all_weeks").attr('checked', false);
                $(".even_weeks").prop('checked', $(this).prop('checked'));
                $("#allWeeks").attr('checked', false);
                $("#defWeeks").attr('checked', false);
                $("#oddWeeks").attr('checked', false);
            });

            $("#clearWeeks").click(function () {
                $(".all_weeks").attr('checked', false);
                $("#allWeeks").attr('checked', false);
                $("#defWeeks").attr('checked', false);
                $("#oddWeeks").attr('checked', false);
                $("#evenWeeks").attr('checked', false);
            });
        });

感谢任何帮助

UPDATED Problem

【问题讨论】:

  • 可以使用$(this)引用触发事件的目标对象。
  • @Alon,我将如何实现,因为如果至少有一个复选框被选中,我需要它用勾号替换叉号,如果没有,则再次用叉号替换勾号他们被检查了

标签: javascript jquery html


【解决方案1】:

我不能说你想要做什么很明显,但可以肯定的是,在复选框元素上调用 .val() 不会得到你想要的结果。这是获取应该工作的空(如未选中)元素数组的好方法:

var empty = $('.weeks').filter(':not(:checked)');

您随后明确表示您想知道是否选中了任何框,如果选中,则显示选中标记;否则显示“X”。因此,您应该得到一个已检查元素的数组,而不是空元素:

var checked = $('.weeks').filter(':checked');

然后相应地显示/隐藏图像:

checked.length ? tick.show() : tick.hide();
checked.length ? cross.hide() : cross.show();

【讨论】:

  • 谢谢,这有效,但只有当我删除我在上面添加的另一段代码时。如果我留下我需要的以前的代码,它就不起作用。
  • 好的。在每个点击处理函数的末尾添加: $('.weeks:first').trigger('change');
猜你喜欢
  • 2021-10-17
  • 2017-05-08
  • 2014-04-02
  • 1970-01-01
  • 2019-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多