我想制作一个 select
标签列表,它们有两个值:yes
和 no
。
我已经创建了它(在 php 中使用 while
语句)。
现在,我想创建一个具有两个值(yes
和 no
)的 select
标签,它应该更改所有 select
tags.ex:如果我选择yes
选项,则所有选项都必须为yes
。
<input onclick="toggle(this)" id="#select-all" type="checkbox" name="one"/><p>all</p>
<select data='beauty' name="txt" id="txt" onchange="setSelectBoxByText(this.getAttribute('data'), this.options[this.selectedIndex].text);">
<option value="id_yes">yes</option>
<option value="id_no">no</option>
</select>
<br />
<br />
<br />
<table style="width:100%">
<?php
$result = mysql_query("my query...");
while($row=mysql_fetch_object($result)){
echo "<tr>
<td>
<input type='checkbox' name='one'/><p style='display:inline;font-size:14px;color:#fff'><a style='color:#fff' href='#'>$row->title</a></p>
</td>
<td align='left' style='padding-left:2px'>
<select name='beauty' id='beauty'>
<option value='id_yes'>yes</option>
<option value='id_no'>no</option>
</select>
</td>
</tr>
";
}
?>
</table>
最佳答案
您可以在 while 中创建的选择上设置类,并在您想要附加 onchange 事件的选择标记上设置类,该事件将按 className 获取所有元素,并将选择索引设置为与您的选择标记选定索引相同。
<select id="globalSelection" onchange="changeSelection(this)">
<option value="id_yes">yes</option>
<option value="id_no">no</option>
</select>
var selects = document.getElementsByClassName('mySelect');
function changeSelection(globalSelect) {
for (var i = 0, select; select = selects[i]; i++) {
select.selectedIndex = globalSelect.selectedIndex;
}
}
关于javascript - 如何使用 JavaScript 更改所有选择标签的所有选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27345602/