如何使用JavaScript更改所有select标记的所有选项

如何使用JavaScript更改所有select标记的所有选项,javascript,php,Javascript,Php,我想列出选择标签,它们有两个值,yes和no。 我已经创建了它(在php中使用while语句) 现在,我想创建一个带有两个值(yes和no)的select标记,它应该更改所有select标记。例如:如果我选择yes选项,所有选项都必须是yes <input onclick="toggle(this)" id="#select-all" type="checkbox" name="one"/><p>all</p> <select d

我想列出
选择
标签,它们有两个值,
yes
no
。 我已经创建了它(在php中使用
while
语句)

现在,我想创建一个带有两个值(
yes
no
)的
select
标记,它应该更改所有
select
标记。例如:如果我选择
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事件的选择标记上设置类,该事件将按类名获取所有元素,并将选择索引设置为与选择标记所选索引相同

    <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;
        }
    }

对
不
var selects=document.getElementsByClassName('mySelect');
功能更改选择(全局选择){
对于(变量i=0,选择;选择=选择[i];i++){
select.selectedIndex=globalSelect.selectedIndex;
}
}