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

时间:2018-08-17 13:36:19

标签: javascript php laravel

我想用Javascript访问Laravel中数组内部的值,这些值在dropdownlist中,并且我想使用onChange()方法来影响视图。 查看代码:

@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

我要在下面的代码中访问“费用或收入”

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;
    }
}

1 个答案:

答案 0 :(得分:0)

我正在做一些简单的假设。看一下这个例子。

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>