簡體   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