我已经查看了重新选择选择下拉列表的第一个选项并将代码合并到我的代码中的选项。它是第一次按下“addRow”按钮,但不是第二次或后续的“addRow”。相关代码是
$("#selectBox option:first-child").attr("selected", "selected");
完整代码如下:
<?php
$db = mysqli_connect("localhost",$username,$password,$database);
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql="SELECT part_id, part_code, part_descr FROM part";
$result = $db->query($sql);
if (!$result){
echo "no record found in the parts table";
}
$option = '<option value = "">select a part</option>';
while ($row = $result->fetch_assoc()) {
$option .= '<option value = "'.$row['part_id'].'">'.$row['part_code'].' '.$row['part_descr'].$row['part_id'].'</option>';
}
?>
<html>
<head>
<title>Test Drop Down Menu</title>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.js"></script>
</head>
<body >
<form id="form2" name="form2" method="post" enctype="multipart/form-data">
<div id="itemRows">
Quantity: <input type="text" name="add_qty" size="4" />
Part: <select id="selectBox" name="selectBox"
data-placeholder="Choose a Part..."
style="width:350px;" class="chosen-select"
onchange="storePartId();"
>
<?php echo $option; ?>
</select>
<input type="hidden" name="add_part_id" id='add_part_id' />
<input type="hidden" name="add_part_descr" id="add_part_descr" />
<input onClick="addRow(this.form);" type="button" value="Add row" />
(not saved until "Add row" clicked)
</div>
<input type="submit" name="next" value="submit report">
</form>
<script type="text/javascript">
var rowNum = 0;
function addRow(frm) {
rowNum ++;
var row = '<p id="rowNum'+rowNum+'">\n\
Quantity: <input type="text" name="qty[]" size="4" value="'+frm.add_qty.value+'" required> \n\
Part: <input type="text" name="part_descr[]" value="'+frm.add_part_descr.value+'" required> \n\
<input type="hidden" name="part_id[]" value="'+frm.add_part_id.value+'" required> \n\
<input type="button" value="Remove" onclick="removeRow('+rowNum+');"></p>';
jQuery('#itemRows').append(row);
frm.add_qty.value = '';
frm.add_part_descr.value = '';
frm.add_part_id.value = '';
$("#selectBox option:first-child").attr("selected", "selected");
}
function removeRow(rnum) {
jQuery('#rowNum'+rnum).remove();
}
function storePartId() {
var selectBox = document.getElementById("selectBox");
selectedValue = selectBox.options[selectBox.selectedIndex].value;
selectedText = selectBox.options[selectBox.selectedIndex].text;
if (selectedValue !== null){
document.getElementById("add_part_id").value = selectedValue;
document.getElementById("add_part_descr").value = selectedText;
} else {
document.getElementById("add_part_id").value = null;
document.getElementById("add_part_descr").value = null;
}
}
</script>
</body>
</html>
我希望“selectBox”在单击“addRow”按钮后显示文本“选择一个部分”。它是第一次单击而不是随后执行。有什么想法吗?
答案 0 :(得分:0)
您可以将Options none
Satisfy all
Order allow,deny
设置为0以选择第一个选项。
selectedIndex