【问题标题】:Button hide not working as expected in JQuery按钮隐藏在 JQuery 中无法按预期工作
【发布时间】:2018-10-02 16:21:46
【问题描述】:

现在它隐藏和取消隐藏表中的所有行。我只想显示选择的文件。有人可以阐明这里缺少的东西吗?我尝试了多种方法,但仍然让我头疼。

<script type="text/javascript">

    $(document).ready(function () {
        $('input[id^="rvw-"]').change(
                function(){
                    if ($(this).val()) {
                        alert("(this).val():"+$(this).val());
                        alert("input id:"+ $('input[id^="rvw-"]').val());
                        $('input[id^="rvw-"]').attr('hidden',false); 
                    } 
                }
          );
    });

</script>

<table id="reviewList" class="table table-striped table-condensed dt-responsive basic-table" cellspacing="0">
    <thead>
        <tr>
            <th class="text-center">Series</th>
            <th class="text-center">Series Order</th>
            <th class="text-center">Document Name</th>
            <th class="text-center">Actions</th>
        </tr>
    </thead>
    <tbody>
        <c:forEach items="${reviewDocList}" var="row">
            <tr>
                <td>${row.series}</td>
                <td>${row.seriesOrder}</td>
                <td>${row.documentName}</td>
                <td>
                    <form class="letter" docId="${row.id}" action='<c:url value="/letter-download.htm?docId=${row.id}"/>' method="POST" enctype="multipart/form-data">
                        <input type="hidden" name="docId" value="${row.id}"/>
                        <label class="hidden" for="rvw-${row.id}">Choose file for ${row.documentName}</label>
                        <input type="file" name="doc" id="rvw-${row.id}" title="Choose File for ${row.documentName}"/>
                        <input type="submit"  id="rvw-${row.id}" value="Upload ${row.documentName}" hidden="true"/>
                    </form>
                </td>
            </tr>
        </c:forEach>
    </tbody>
</table>

【问题讨论】:

    标签: jquery html css button input


    【解决方案1】:

    问题来自以下行中的选择器:

    $('input[id^="rvw-"]').attr('hidden',false); 
    

    当前选择器input[id^="rvw-"] 将定位所有idrvw- 开头的输入。

    您需要使用$(this) 来引用更改后的文件输入,例如:

    $(this).attr('hidden', false); 
    

    【讨论】:

    • 嗨,Zakaria,它不起作用。现在所有按钮都隐藏了,即使在选择了一行文件之后也是如此。
    • 我不确定你是如何测试它的,但在这里快速检查一下就会告诉你我的意思jsfiddle.net/z_acharki/jnwrc5ay/1304
    • 按钮 id 是唯一的
    • 身份证没问题,没问题
    • 从小提琴中我看不到按钮在以第一种形式选择文件后被取消隐藏。应该是 $('#rvw-$1').attr('hidden', false);但还是不行
    【解决方案2】:

    您是否考虑过改用document.querySelectorelement.classList.toggle('classname') 将不再需要 JQuery

    只是一个想法。如果需要,您也可以使用Babel 将其转换为 ES5。

    【讨论】:

      猜你喜欢
      • 2019-01-08
      • 1970-01-01
      • 2011-06-11
      • 2021-02-27
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多