如何将数组值传递给javascript函数?拉威尔酒店
我在javascript中有一个名为country_id[]的数组,并选择了_country[]。当用户在国家/地区数组中选择国家/地区时,它将保存在国家/地区id[]数组中;当用户在所选国家/地区中选择国家/地区时,它将保存在所选国家/地区[]中。然后,我想将country_id[]和selected_country[]的值传递给脚本函数处的函数。这是将数据从右向左和从左向右移动。照片附在这里 但它说,下面的错误 错误:如何将数组值传递给javascript函数?拉威尔酒店,javascript,php,jquery,html,laravel,Javascript,Php,Jquery,Html,Laravel,我在javascript中有一个名为country_id[]的数组,并选择了_country[]。当用户在国家/地区数组中选择国家/地区时,它将保存在国家/地区id[]数组中;当用户在所选国家/地区中选择国家/地区时,它将保存在所选国家/地区[]中。然后,我想将country_id[]和selected_country[]的值传递给脚本函数处的函数。这是将数据从右向左和从左向右移动。照片附在这里 但它说,下面的错误 错误: 179:575 Uncaught ReferenceError: cou
179:575 Uncaught ReferenceError: country_id is not defined
表格:
<form method="POST" action="{{route('admin-insurance-region-duplicateRegion')}}" onsubmit="return false;">
@csrf
<input name="id" value="{{$region->id}}" type="hidden" class="form-control" required>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Choose Country</label>
<select class="form-control" name="country_id[]" multiple size="10" style="height: 100%;">
@foreach ($countries as $item)
<option value="{{$item->id}}" selected>{{$item->name}}</option>
@endforeach
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Selected Country</label>
<select class="form-control" name="selected_country[]" multiple size="10" style="height: 100%;">
@foreach ($country_region as $item)
<option value="{{$item->id}}" selected>{{$item->name}}</option>
@endforeach
</select>
</div>
</div>
<button onclick="moveRight()">Move option to the right</button>
<button onclick="moveLeft()">Move option to the Left</button>
</div>
JAVASCRIPT:
<script>
function moveRight() {
var selItem = document.forms[0].country_id.selectedIndex;
if (selItem == -1) {
window.alert("You must first select an item on the left side.")
} else {
document.forms[0].selected_country.add(document.forms[0].country_id[selItem], null);
}
}
function moveLeft() {
var selItem = document.forms[0].selected_country.selectedIndex;
if (selItem == -1) {
window.alert("You must first select an item on the left side.")
} else {
document.forms[0].country_id.add(document.forms[0].selected_country[selItem], null);
}
}
</script>
在“国家”和“选定国家”选择框中添加Id属性 喜欢 演示 选择国家 1. 2. 3. 选定国家 s1 s2 s3 将选项移到右侧 向左移动选项 功能右移{ var selItem=document.forms[0]。国家/地区id.selectedIndex; 如果selItem==-1{ window.alert必须首先选择左侧的项目。 }否则{ document.forms[0]。所选国家/地区.adddocument.forms[0]。国家/地区id[selItem],空; } } 函数左移{ var selItem=document.forms[0]。所选国家/地区。所选索引; 如果selItem==-1{ window.alert必须首先选择左侧的项目。 }否则{ document.forms[0]。国家/地区id.adddocument.forms[0]。所选国家/地区[selItem],空; } }
<select class="form-control" id="country_id" name="country_id[]" multiple size="10">
-----
</select>
<select class="form-control" id="selected_country" name="selected_country[]" multiple size="10">
-----
</select>