drake900918 2018-08-17 13:36
浏览 236
已采纳

如何从javascript访问Laravel中的数组中的值

I want to access values inside array in Laravel with Javascript and those values are in dropdownlist and I want to use onChange() method to influence the view. The view code:

@extends('layouts.app')
@section('content')
<h1>Online Accounting</h1>
<br>
{!! Form::open(['action'=>'IncomeExpenseController@store', 'method' =>'POST']) !!}

    <div class="form-group">
     {{Form::label('IncomeExpense', 'IncomeExpense')}}
     {{Form::select('IncomeExpense', array('Expense'=> 'Expense', 'Income'=>'Income'),null,['class'=>'form-control')}}
    </div>
    <div class="form-group">
            {{Form::label('', 'Income')}} 

            {{Form::select('IncomeId', $incomes,null,['class'=>'form-control',,'id'=>'Income',  'onChange'=>'validate()'])}}
           </div>
           <div class="form-group">
                {{Form::label('', 'Expense')}}           
                {{Form::select('ExpenseId', $expenses,null,['class'=>'form-control', 'id'=>'Expense'])}}
               </div>
<div class="form-group">
           {{Form::label('SumOfMoney', 'Money')}}
    {{Form::text('SumOfMoney', '', ['class' =>'form-control'])}}
</div>
<div class="form-group">
        {{Form::label('Comments', 'Comments')}}
        {{Form::textarea('Comments', '', ['class' =>'form-control'])}}

        </div>
    {{Form::submit('Add', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
@stop

I want to access Expense or Income in above code for below code

Javascript code:

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;
    }
}
  • 写回答

1条回答 默认 最新

  • duanbangzhou7809 2018-08-17 14:00
    关注

    I and doing some simple assumptions. Take a look at this example.

    var selector = document.querySelector('#selector');
    var income = document.querySelector('#income');
    var expense = document.querySelector('#expense');
    
    function onChange() {
      expense.disabled = selector.value === 'selectoption' || selector.value === 'income';
      income.disabled = selector.value === 'selectoption' || selector.value === 'expense';
    }
    
    selector.onchange = onChange;
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
    </head>
    <body>
      <select id="selector">
        <option value="selectoption">Select an Option</option>
        <option value="income">Income</option>
        <option value="expense">Expense</option>
      </select>
      <select id="income" disabled="disabled">
        <option value="selectincome">Select an Income</option>
        <option value="foo">Foo</option>
        <option value="bar">Bar</option>
        <option value="baz">Baz</option>
      </select>
      <select id="expense" disabled="disabled">
        <option value="selectexpense">Select an Expense</option>
        <option value="foo">Foo</option>
        <option value="bar">Bar</option>
        <option value="baz">Baz</option>
      </select>
    </body>
    </html>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度