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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?