gpt4 book ai didi

javascript - 如何将数组值传递给javascript函数? PHP Laravel

转载 作者:行者123 更新时间:2023-12-02 21:47:19 25 4
gpt4 key购买 nike

我在 JavaScript 中有一个数组,名为country_id[]和selected_country[]。当用户在country数组中选择国家时,它将保存在country_id[]数组中,当用户在所选国家/地区中选择国家时,它将保存在selected_country[]中。然后,我想将country_id[]和selected_country[]的值传递给脚本函数中的函数。这是将数据从右到左、从左到右移动。图片附在这里。 enter image description here

但它说,下面有错误。

错误:

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 属性。

喜欢。

<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>

演示。

<form method="POST" action="{{route('admin-insurance-region-duplicateRegion')}}" onsubmit="return false;">
<label>Choose Country</label>
<select class="form-control" id="country_id" name="country_id[]" multiple size="10">
<option value="1" selected>1</option>
<option value="2" selected>2</option>
<option value="3" selected>3</option>
</select>
<label>Selected Country</label>
<select class="form-control" id="selected_country" name="selected_country[]" multiple size="10">
<option value="s1" selected>s1</option>
<option value="s2" selected>s2</option>
<option value="s3" selected>s3</option>
</select>
</div>
</div>
<button onclick="moveRight()">Move option to the right</button>
<button onclick="moveLeft()">Move option to the Left</button>
</div>

<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>

关于javascript - 如何将数组值传递给javascript函数? PHP Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60220609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com