无法从选择中获取值(JS)
2018-04-02
53
我有包含多个选择的表单
这是代码
<form>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Ф.И.О</label>
<input type="text" class="form-control" id="Name">
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Дата Рождения</label>
<input type="date" class="form-control" id="birthday">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Должность:</label>
<textarea class="form-control" id="position"></textarea>
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Телефон:</label>
<textarea class="form-control" id="telephonepeople"></textarea>
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">График работы:</label>
<select class="form-control" id="workTime">
<option>Дневная Смена</option>
<option>Ночная смена</option>
<option>Сутки</option>
<option>Дневная -Ночная смены</option>
</select>
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Адрес проживания:</label>
<textarea class="form-control" id="adress"></textarea>
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Регион:</label>
@Html.DropDownList("Region", null, htmlAttributes: new { @class = "form-control", @id = "region" })
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Паспортные данные:</label>
<textarea class="form-control" id="passportData"></textarea>
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">ИНН:</label>
<textarea class="form-control" id="INN"></textarea>
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Медицинская книга:</label>
<select class="form-control" id="medicalCard">
<option>Имеется</option>
<option>Не Имеется</option>
</select>
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Дата окончания мед книжки:</label>
<input type="date" class="form-control" id="bookending">
</div>
我需要从中获取值并传递给后端。
我有这个 javascript 代码(这里是我获取值的部分,因为其他所有代码都运行良好)
function AddPeople() {
let proposalurl = '/peopleforworks/addpeople';
let Name = $('#Name').val();
let birthday = $('#birthday').val();
let telephone = $('#telephonepeople').val();
let workTime = $('#workTime').text();
let adress = $('#adress').val();
let passportData = $('#passportData').val();
let medicalCard = $('#medicalCard').val();
let INN = $('#INN').val();
let medicalbookdate = $('#bookending').val();
let position = $('#position').val();
let region = $('#region').text();
alert(workTime);
但警报没有显示任何内容。所有其他选择都在运行,并且值正在从它们中获取。
我的问题可能出在哪里?
谢谢帮助
2个回答
您需要为每个
select
的
<option>
定义一个值:
替换此值:
<select class="form-control" id="workTime">
<option>Дневная Смена</option>
<option>Ночная смена</option>
<option>Сутки</option>
<option>Дневная -Ночная смены</option>
</select>
替换为:
<select class="form-control" id="workTime">
<option value="Дневная Смена">Дневная Смена</option>
<option value="Ночная смена">Ночная смена</option>
<option value="Сутки">Сутки</option>
<option value="Дневная -Ночная смены">Дневная -Ночная смены</option>
</select>
以及:
let workTime = $('#workTime').text();
替换为:
let workTime = $('#workTime').val();
Thomas Brd
2018-04-02
使用
.val()
让 workTime = $('#workTime').val();
Matt
2018-04-02