Javascript 如果选择了下拉列表中的任何选项,则需要输入
我有一个发票表单,它包含一个由3列组成的表:kode_barang ItemID、nama_barang ItemName和qtyquantity。假设它只有2行:Javascript 如果选择了下拉列表中的任何选项,则需要输入,javascript,php,jquery,Javascript,Php,Jquery,我有一个发票表单,它包含一个由3列组成的表:kode_barang ItemID、nama_barang ItemName和qtyquantity。假设它只有2行: <form name="invoice" action="insert3.php" method="post"> <table id="theTable" border="1"> <thead> <tr> <th> Kode Bara
<form name="invoice" action="insert3.php" method="post">
<table id="theTable" border="1">
<thead>
<tr>
<th> Kode Barang </th>
<th> Nama Barang </th>
<th> Qty </th>
</tr>
</thead>
<tbody>
<tr>
<td><select name="kode_barang[]" id="kode_barang0" required /> </td>
<td><input type="text" name="nama_barang[]" id="nama_barang0" required /></td>
<td><input type="text" name='qty[]' required /></td>
</tr>
<tr>
<td><select name="kode_barang[]" id="kode_barang1" /> </td>
<td><input type="text" name="nama_barang[]" id="nama_barang1" /></td>
<td><input type="text" name='qty[]' /></td>
</tr>
</tbody>
</table>
有人能帮我吗?谢谢。这是您想要的,但有两个简单字段。如果你需要帮助了解任何事情,请告诉我
<script type="text/javascript">
function isMandatory(){
var dropDownElement = document.getElementById('dropdown');
if(!dropDownElement){ // check if dropdown element exists
return false;
}
if(dropDownElement.value == ""){ // check if value is selected in dropdown
return false;
}
return true;
}
function submitForm(){
var fieldElement = document.getElementById('someField');
if(!fieldElement){ // check if textfield element exists
return;
}
if(isMandatory() && fieldElement.value == ""){
alert("Field is mandatory!"); // or show message in a div
}
else{
// do something
}
}
</script>
<div>
<select id="dropdown">
<option value="">--select--</option>
<option value="item-1">item-1</option>
<option value="item-2">item-2</option>
</select>
<input id="someField" type="text" />
<input type="button" value="submit" onclick="submitForm();" />
</div>
必须指定包含特殊字符的对象属性名称quoted@charlietfl仍然没有运气:所以,考虑一个简单的例子,你的问题是:如果下拉是肯定的,你想要另一个文本字段是强制性的,否则。我说得对吗?@RupinderSingh只需从下拉列表中选择任意选项,另一个文本字段为必填字段,否则就不行。效果非常好。谢谢
<script type="text/javascript">
function isMandatory(){
var dropDownElement = document.getElementById('dropdown');
if(!dropDownElement){ // check if dropdown element exists
return false;
}
if(dropDownElement.value == ""){ // check if value is selected in dropdown
return false;
}
return true;
}
function submitForm(){
var fieldElement = document.getElementById('someField');
if(!fieldElement){ // check if textfield element exists
return;
}
if(isMandatory() && fieldElement.value == ""){
alert("Field is mandatory!"); // or show message in a div
}
else{
// do something
}
}
</script>
<div>
<select id="dropdown">
<option value="">--select--</option>
<option value="item-1">item-1</option>
<option value="item-2">item-2</option>
</select>
<input id="someField" type="text" />
<input type="button" value="submit" onclick="submitForm();" />
</div>