dsfh40613182 2018-08-16 16:41
浏览 175
已采纳

如何确保在一个下拉列表中选择的值将更改Laravel中另一个下拉列表的值

I was developing Laravel project and what I want to achieve is that when a user picks a certain value in dropdownlist, it should influence other dropdownlists in the view, that is, if user selects Income in the first dropdown then Expense dropdownlist should be disabled or something like that Controller

>  public function create()
>     {
>         $income = Income::pluck('title', 'id');
>         $expense=Expenses::pluck('title', 'id');
>         return view ('IncomeExpense.create', compact('income','expense'));
>     }

View

<div class="form-group">
>      {{Form::label('IncomeExpense', 'Income&Expense')}}
>      {{Form::select('IncomeExpense', array('E' => 'Expense', 'I' => 'Income'),null,['class'=>'form-control'])}}
>     </div>
>     <div class="form-group">
>             {{Form::label('', 'Income')}}           
>             {{Form::select('IncomeId', $income,null,['class'=>'form-control'])}}
>            </div>
>            <div class="form-group">
>                 {{Form::label('', 'Expense')}}           
>                 {{Form::select('ExpenseId', $expense,null,['class'=>'form-control'])}}
>                </div>
  • 写回答

2条回答 默认 最新

  • drxdn40242 2018-08-16 20:55
    关注

    I got a solution for you. But you may use select('title', 'id')->get() instade of pluck('title', 'id'). Because pluck is generally used for geting a single column value.

    Now, for your solution, you can use a little bit of JavaScript. For that, you have to give an id to your select tags each. Like

    Income Dropdown

    <select class="form-control select2" id="income" onChange="validate()">
      <option value="selectincome">--- selectincome ---</option>
      @foreach($incomes as $income)
      <option value="{{$income['id']}}">{{$income['title']}}</option>
      @endforeach
    </select>
    

    And Expense Dropdown

    <select class="form-control select2" id="expense" onChange="validate()">
      <option value="selectexpence">--- selectexpence ---</option>
      @foreach($expenses as $expense)
      <option value="{{$expense['id']}}">{{$expense['title']}}</option>
      @endforeach
    </select>
    

    Now, the validate() method

    JavaScript

    function validate(){
        var ddlIncome = document.getElementById("income");
        var ddlExpence = document.getElementById("expense");
    
        var selectedValueIncome = ddlIncome.options[ddlIncome.selectedIndex].value;
        var selectedValueExpence = ddlExpence.options[ddlExpence.selectedIndex].value;
    
        if (selectedValueIncome != "selectincome"){
            document.getElementById("expense").disabled=true;
        }
        else if (selectedValueIncome != "selectexpence"){
            document.getElementById("income").disabled=true;
        }
    }
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?