[英]How to access values in array in Laravel from javascript
我想使用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;
}
}
我並做了一些簡單的假設。 看一下這個例子。
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.