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

如何从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>
    
    点赞 打赏 评论

相关推荐 更多相似问题