![](/img/trans.png)
[英]Select only one checkbox (and deselect rest) and show input field if checkbox selected
[英]Require input field to be filled only if one of multiple select options selected
我有一個帶有SELECT和TEXT(和其他)輸入的表單。 SELECT有兩個選項。 其中一個選項要求將日期添加到文本框中,另一個不需要日期。
我需要找到一種方法,如果選擇了一個表單選項,但不要求其他輸入,則要求完成TEXT輸入(這是一個jquery datepicker)。
表格 ...
<input class="ml-text" type="text" name="descriptive_title" size="45" placeholder="Required ..." value="<? echo $doc_title; ?>"required>
<br><br>
<label class="title">Upload Type</label>
<select class= "ml-select" name="upload_type">
<option value="cutting_list">Cutting List</option>
<option value="site_instruction">Extra Works</option>
</select>
<input class="ml-text" type="text" size="22" name="date_required" id="datepicker" placeholder="Date Required">
我嘗試使用處理表單的頁面上的標頭來執行此操作,但是與所有標頭一樣,尤其是在同一頁面上使用兩個標頭時,似乎在執行其他任何操作之前先搶了頭。
我試過了 ...
if($upload_type == 'cutting_list' && !isset($POST['date_required'])){
$desc_title = str_replace(' ', '_', $descriptive_title);
header( "Location: ../workflow/workflow.php?error=no_date&doc_title=$desc_title&department=$department");
}
else {
.... do everything else
header( "Location: ../workflow/workflow.php ");
}
這樣的想法是,如果上傳類型是剪切列表,但未設置日期POST(也嘗試!empty),則它將返回錯誤的表單,但是如果不滿足這些條件,則將繼續執行腳本並返回到表單,現在提交該表單,標題中沒有錯誤內容。
只是無法使其正常工作。
您可以使用javascript / jQuery檢查select
( https://api.jquery.com/change/ )的值,如果該值具有必需的日期時間,則可以添加required
( https://www.w3。 org / TR / html5 / forms.html#the-required-attribute )屬性input
小樣本腳本,如果選擇值1
,它將把輸入設置為必填。
<html> <body> <select onchange="if (this.options[this.selectedIndex].value == '1') {document.getElementById('myinput').setAttribute('required', '');} else {document.getElementById('myinput').removeAttribute('required')}"> <option value=''>none</option> <option value=1>1</option> <option value=2>2</option> </select> <input type='text' id='myinput'> </body> </html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.