繁体   English   中英

使用jQuery更改名称属性的值

[英]change value of name attribute using jquery

我在表中打印数据,并尝试更改特定hidden field名称,但我不能将我的代码如下

        <table class="table table-hover">
            <thead>
            <tr>
                <th>
                    <input type="checkbox">
                </th>
                <th>sr no</th>
                <th>seller</th>
                <th>shape</th>
                <th>size</th>
                <th>color</th>
                <th>discount</th>
                <th>client</th>
            </tr>
            </thead>
            <tbody>
            @foreach($cod_details as $cod)
                <tr>
                    <td>
                        <input type="checkbox" name="ids[]" id="{{ $cod->id }}" value="{{ $cod->id }}">
                        <input type="hidden" value="{{ $cod->report_no }}" id="{{ $cod->report_no }}" name="" class="report">
                    </td>
                    <td>{{ $cod->id }}</td>
                    <td>{{ $cod->seller }}</td>
                    <td>{{ $cod->shape }}</td>
                    <td>{{ $cod->size }}</td>
                    <td>{{ $cod->color }}</t
                    <td>{{ $cod->discount }}</td>
                    <td>{{ $cod->client }}</td>
                </tr>
            @endforeach
            </tbody>
        </table>

这显示了表格中的所有细节,并且工作完美。

$(document).ready(function(e){
        $('input[type="checkbox"]').click(function(){
            if($(this).prop("checked") == true){
                $(this).closest('input[type="hidden"]').attr('name','report_no[]');

            }
            else{

            }
        });
    });

我尝试在选中复选框时添加name = report_no[] ,但我不能。

复选框之后,它是DOM中的下一个元素。 因此,在您的处理程序中:

var input = $(this).next();

...将为您提供该input字段的jQuery包装器。

旁注: if($(this).prop("checked") == true){是一种写if (this.checked)漫长方法,我只是使用它。 :-)

所以:

$('input[type="checkbox"]').click(function() {
    if (this.checked) {
        $(this).next().attr('name','report_no[]');
    }
    else {

    }
});

如果您想在未选中此复选框时清除它,则:

$('input[type="checkbox"]').click(function() {
    $(this).next().attr("name", this.checked ? "report_no[]" : "");
});

暂无
暂无

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

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