Jquery更改不起作用
我有一个选择选项,有两个值本地/导入。每个值都有一个对应的表,当选择该表时,该表将显示在页面上。我使用了jquerychange,但它不起作用,而是在页面中显示所有表。请帮助我我的代码有什么问题。谢谢 以下是jquery代码:Jquery更改不起作用,jquery,html,Jquery,Html,我有一个选择选项,有两个值本地/导入。每个值都有一个对应的表,当选择该表时,该表将显示在页面上。我使用了jquerychange,但它不起作用,而是在页面中显示所有表。请帮助我我的代码有什么问题。谢谢 以下是jquery代码: <script> $('#shiptype').change(function () { $('#t1,#t2').hide(); var tableValue = $(this).val(); if (tableValue ==
<script>
$('#shiptype').change(function () {
$('#t1,#t2').hide();
var tableValue = $(this).val();
if (tableValue == 1) {
$('#t1').show();
} else if (tableValue == 2) {
$('#t2').show();
}
});
</script>
下面是html部分:
<form name='form1' method='post' autocomplete='on'>
<table id='tblselect' class=normal2 style='font-size:0.6em;'>
<tr><th align='right'>Shipment Type: </th>
<td><select name='shiptype' id="shiptype" onchange="">
<option value="0"> Please select... </option>
<option value="1"> LOCAL </option>
<option value="2"> IMPORT </option>
</select></td></tr>
</table>
<br>
<table id="t1" class=normal2 style='font-size:0.6em;'>
<tr><th align='right'>Shipment Type:</th>
<td><b><input type='text' name='ship' value='LOCAL' readonly /></b></td></tr>
<tr><th align='right'>Reference Number: </th>
<td><b><input type='text' name='ref_no' value="<?php echo $localrefnumb ?>" readonly /></b></td></tr>
<tr><th align='right'>Supplier: </th>
<td><select name='supplier'>
<option value=' '> Please select... </option>
<option value='Supplier 1'> Supplier 1 </option>
<option value='Supplier 2'> Supplier 2 </option>
<option value='Supplier 3'> Supplier 3 </option>
</select></td></tr>
<tr><th align='right'>Description: </th>
<td><input type='text' name='description' size='70' /></td></tr>
<tr><th align='right'>PO No: </th>
<td><input type='date' name='po_no' /></td></tr>
<tr><th align='right'>Invoice Number: </th>
<td><input type='text' name='inv_number' /></td></tr>
<tr><th align='right'>Receive Qty: </th>
<td><input type='text' name='r_qty'/></td></tr>
<tr><th align='right'>No. of Boxes/Packs: </th>
<td><input type='text' name='no_boxes'/></td>
<tr><th align='right'> </th>
<td></td></tr>
<tr><th align='right'> </th>
<td align='left'><input type='submit' name='local_btn' value='SAVE' style='font-size:2em;'> <input type='submit' name='local_cancel' value='CANCEL' style='font-size:2em;'></td></tr>
</table>
<table id="t2" class=normal2 style='font-size:0.6em;'>
<tr><th align='right'>Shipment Type:</th>
<td><b><input type='text' name='ship2' value='IMPORT' readonly /></b></td></tr>
<tr><th align='right'>Reference Number: </th>
<td><b><input type='text' name='ref_no2' value="<?php echo $importrefnumb ?>" readonly /></b></td></tr>
<tr><th align='right'>Supplier: </th>
<td><select name='supplier2'>
<option value=' '> Please select... </option>
<option value='Supplier1'> Supplier 1 </option>
<option value='Supplier2'> Supplier 2 </option>
<option value='Supplier3'> Supplier 3 </option>
</select></td></tr>
<tr><th align='right'>Description: </th>
<td><input type='text' name='description2' size='70' /></td></tr>
<tr><th align='right'>PO No: </th>
<td><input type='date' name='po_no2' /></td></tr>
<tr><th align='right'>Invoice Number: </th>
<td><input type='text' name='inv_number2' /></td></tr>
<tr><th align='right'>AWB(Airway Bill) No: </th>
<td><input type='text' name='awb_no'/></td></tr>
<tr><th align='right'>AWB(Airway Bill) No of Boxes/Packs: </th>
<td><input type='text' name='awb_no_boxes'/></td></tr>
<tr><th align='right'> </th>
<td></td></tr>
<tr><th align='right'> </th>
<td align='left'><input type='submit' name='import_add' value='SAVE' style='font-size:2em;'> <input type='submit' name='import_cancel' value='CANCEL' style='font-size:2em;'></td></tr>
</table>
</form>
你的代码对我有用。我刚刚添加了$'t1,t2'。隐藏;在装载时隐藏它们。 工作小提琴 可能会将所有代码放在就绪函数中:
$(document).ready(function(){
//put all your JS code here
});
您需要将表设置为显示:否则它们将不会显示在页面中,或者您也可以在页面加载时隐藏它们。您还需要将JS放入$document.ready函数中
<table id="t1" class=normal2 style='font-size:0.6em;display:none;'>
<table id="t2" class=normal2 style='font-size:0.6em;display:none;'>
下面是一个相同的示例。将jQuery代码包装到就绪处理程序中,或者将脚本移动到正文的末尾。Remove onchange=from@Kimlyn Tormes原因是您没有隐藏在文档准备就绪或加载中。谢谢@Tushar。我在正文末尾移动脚本