jQuery只显示空白文本

时间:2017-06-23 16:54:58

标签: javascript jquery laravel

我目前遇到了一个问题。问题是,当尝试从按钮上方的2个类中获取数据时,它会准确返回此' '

这就是app.js的样子:

$('.edit').on('click', function(event) {
    event.preventDefault();
    var productNamee = event.target.parentNode.childNodes[1].textContent;
    $('#productname').val(productNamee);
    $('#myModal').modal('show'); 
});

这就是我的代码实现方式:

<table class="table table-striped table-bordered">
    <tr>
        <th>Product Name</th>
        <th>Brand</th> 
        <th>Description</th>
        <th>Posted On</th>
        <th>Actions</th>
    </tr>
    @foreach($posts as $post)
    <tr>
        <div class="postcontest">
            <td>{{ $post->productname }}</td>
            <td>{{ $post->brands }}</td> 
            <td>{{ $post->description }}</td>
            <td>{{ $post->created_at }}</td>
        </div> 
        <td>
            <div class="interaction">
                <a class="btn btn-warning btn-sm edit" role="button" href="#"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></span></a>
                <a class="btn btn-danger btn-sm" href="{{ route('post.delete', ['post_id' => $post->id]) }}" role="button"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></a>
            </div>
        </td>
    </tr>                           
    @endforeach
</table>

这是打开模态时返回的内容: https://gyazo.com/b5709533810d4bdb95029db3af8a9c4c(完全没有!)

模态:

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Edit a product</h4>
        </div>
        <div class="modal-body">
            <form action="#" method="post">
                <div class="form-group">
                    <label for="name">Product Name</label>
                    <input class='form-control' type="text" name="productname" id="productname">
                </div>
                <div class="form-group">
                    <label for="brands">Brands</label>
                    <input class='form-control' type="text" name="brands" id="brands">
                </div>
                <div class="form-group">
                    <label for="brands">Cop Link</label>
                    <input class='form-control' type="text" name="coplink" id="coplink">
                </div>
                <div class="form-group">    
                    <label for="brands">Description</label>                     
                    <textarea class="form-control" name="description" id="description" rows="5"></textarea>
                </div>
                <input type="hidden" name="_token" value="{{ Session::token() }}">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Save changes</button></form>
            </div>
        </div>
    </div>
</div>

是的,文本字段的ID是'productname',这一切都是正确的。

我目前没有关于如何解决此问题的想法,任何帮助都将不胜感激!

1 个答案:

答案 0 :(得分:0)

更改此代码:

C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0

<a class="btn btn-warning btn-sm edit" role="button" href="#"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></span></a>

<a onClick="edit('{{$post->productname}}',
                 '{{$post->brands}}',
                 '{{$post->description}}', 
                 '{{$post->created_at}}')," 
                 class="btn btn-warning btn-sm edit" role="button" href="#"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></span></a>

$('.edit').on('click', function(event) {
    event.preventDefault();
    var productNamee = event.target.parentNode.childNodes[1].textContent;
    $('#productname').val(productNamee);
    $('#myModal').modal('show'); 
});