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

如何从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条回答 默认 最新

相关推荐 更多相似问题