存储值后,重复重新选择选择下拉菜单的第一个选项
Reselect first option of a select dropdown menu repeatedly after storing value
我已经查看了重新选择下拉列表的第一个选项的选项,并将代码包含在我的代码中。它在我第一次按下"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"按钮后显示文本"select a part"。它在第一次单击时执行,但随后不会执行。有什么想法吗?
您可以将selectedIndex
设置为0以选择第一个选项。
document.getElementById('selectBox').selectedIndex = 0
相关文章:
- 从选择下拉菜单中获取数据
- 在angularjs UI网格列中选择下拉菜单不适用于外部editcellTemplate
- JS在选择下拉菜单打开时创建延迟
- 选择下拉菜单:实现非精确搜索,每个单词都将被不可重复地搜索
- 在javascript中动态添加一个选择下拉菜单
- html表单上的许多选择(下拉菜单),如何只获取更改的选择的值
- 未选择下拉菜单时更改css
- 存储值后,重复重新选择选择下拉菜单的第一个选项
- 用于关闭和打开的Jquery事件选择下拉菜单,而不是在更改时
- 是否可以在HTML选择下拉菜单的每个选项上附加一个qtip2工具提示
- 如何使用选择下拉菜单更改元素
- 圆角<选择>下拉菜单
- 在选择下拉菜单时创建文本字段
- HTML选择下拉菜单,最大高度为100%,大小自动
- <选择>下拉菜单以定位JavaScript打开的文件
- 将数据加载到动态填充的分部视图中的选择下拉菜单
- 表排序器从选择下拉菜单和自定义时间过滤器中选择多个选项
- angularjs-编辑项目时,在选择下拉菜单中设置所选值
- 输入-选择下拉菜单在firefox中不起作用
- AngularJS-为什么选择下拉菜单;零钱上没有$event