【问题标题】:Laravel : How to get anchor tag value in variable and use this variable in the input field?Laravel:如何在变量中获取锚标记值并在输入字段中使用该变量?
【发布时间】:2017-11-27 17:52:50
【问题描述】:

而这个$emp->id 给了我一个准确的id,所以我只是从中得到id={{$emp->id}} 不是问题吗?请帮忙。

<a href="{{'/employee'}}?id={{$emp->id}}" type="button" name="u_id" > Apply Attribute </a>

单击应用按钮后的值,使用它总是返回第一个用户 id,或者如果我在 foreach 循环之外使用它,它会给我第二个用户 id,所以如果我们在点击 href 时得到想要的将此 id 值存储在变量中并在值中使用它?所以请帮我从锚标签中获取价值?如果你解决了这个问题,我很高兴。提前感谢开发人员。

<form action="{{'/rating'}}" method="post">
 {{csrf_field()}} 

 <input type="hidden" name="user_id" value="{{$emp->id}}" />

 <input type="submit"  style="margin-bottom: 10px;" class="btn btn-success pull-right" name="apply" value="Apply"/>

</form>

【问题讨论】:

  • foreach 循环在哪里?以及您从哪里获得这些值?
  • @foreach($employ as $emp) {{$emp->name}} } ?> 应用属性
  • 编辑问题请提供更多详细信息,以便我们提供帮助。
  • 我没有获得价值,但是当我将鼠标滚动到“应用属性”按钮后,我们会显示在底部,然后会显示在窗口底部,
  • 好的,我会编辑问题

标签: javascript php jquery laravel laravel-5.5


【解决方案1】:

你为什么要拉 {{'/employee'}} ?

@foreach($employ as $emp)
   <tr> 
      <td>{{$emp->name}}</td>
   <td> 
      <a href="/employee?id={{$emp->id}}" type="button" name="u_id"class="btn btn-primary" data-toggle="modal" data-target="#myModal"> 
         Apply Attribute 
      </a>
   </td>
@endforeach

【讨论】:

    【解决方案2】:

    Href 那样:

    <a href="{{$emp->id}}" type="button"  id="uu_id" class="btn btn-primary uu" 
    data-toggle="modal" data-target="#myModal"> Apply Attribute </a>
    

    使用此脚本从 Href 获取值:

    <script type="text/javascript">
    $(document).ready(function() {
        $(".uu").click(function(event) {
            var u_id = $(this).attr('href');
            event.preventDefault();
            document.getElementById("hiddenVal").value = u_id;
        });
    
     });
    </script>
    

    在你的表单中是这样的:

    <form action="{{'/rating'}}" method="post">
     {{csrf_field()}} 
    
     <input type="submit"  style="margin-bottom: 10px;" class="btn btn-success 
       pull-right" name="apply" value="Apply"/>
     <input type="hidden" name="hiddenVal" id="hiddenVal" />
    </form>
    

    最后如何在控制器中获取这个值并保存到数据库:

    public function store(Request $request)
     {
       $rates = new Rating;
       $user_id = $_POST['hiddenVal'];
       $rates->user_id = $user_id;    
       $rates->save();
       return redirect('/employee');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-19
      • 2017-04-18
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      相关资源
      最近更新 更多