推送数组jQuery中的值

时间:2014-06-24 07:54:27

标签: jquery arrays

我需要一个数组,它只存储连续位置的给定选项中的选定值。当我运行下面的代码时,所有值都存储在同一位置(0),而数组的其余部分都是空的。

以下是我的HTML:

<div>
        <table id="myTable">
            <tbody>
                <tr>
                    <th>Robot By</th>
                    <th>Robot Name</th>
                    <th>Status</th>
                    <th>ADD</th>
                    <th>REMOVE</th>
                </tr>
                <tr id="col1">
                    <td>Google</td>
                    <td><em>googlebot</em></td>
                    <td>
                    <select class="myOption">
                        <option value="Same as Default">Same as Default</option>
                        <option value="Allowed">Allowed</option>
                        <option value="Refused">Refused</option>
                    </select></td>
                    <td>
                    <button type="button" class="addC">
                        Add
                    </button></td>
                    <td>
                    <button type="button" class="removeC">
                        Remove
                    </button></td>
                </tr>
                <tr id="col2">
                    <td>MSN Search</td>
                    <td><em>msnbot</em></td>
                    <td>
                    <select class="myOption">
                        <option value="Same as Default">Same as Default</option>
                        <option value="Allowed">Allowed</option>
                        <option value="Refused">Refused</option>
                    </select></td>
                    <td>
                    <button type="button" class="addC">
                        Add
                    </button></td>
                    <td>
                    <button type="button" class="removeC">
                        Remove
                    </button></td>
                </tr>
                <tr id="col3">
                    <td>Yahoo</td>
                    <td><em>yahoo-slurp</em></td>
                    <td>
                    <select class="myOption">
                        <option value="Same as Default">Same as Default</option>
                        <option value="Allowed">Allowed</option>
                        <option value="Refused">Refused</option>
                    </select></td>
                    <td>
                    <button type="button" class="addC">
                        Add
                    </button></td>
                    <td>
                    <button type="button" class="removeC">
                        Remove
                    </button></td>
                </tr>
                <tr id="col4">
                    <td>Ask/Teoma</td>
                    <td><em>teoma</em></td>
                    <td>
                    <select class="myOption">
                        <option value="Same as Default">Same as Default</option>
                        <option value="Allowed">Allowed</option>
                        <option value="Refused">Refused</option>
                    </select></td>
                    <td>
                    <button type="button" class="addC">
                        Add
                    </button></td>
                    <td>
                    <button type="button" class="removeC">
                        Remove
                    </button></td>
                </tr>
            </tbody>
        </table>
    </div>
            <input type="button" value="Create Robots.txt" id="createFile">
            <input type="reset" value="Clear All" id="clearAll">

以下是我的jQuery:

$("#createFile").click(function() {

    $('#myTable tr').find('td').each(function(index) {
        $(this).find('.myOption').each(function(){

            var myArray = [];
            myArray.push($(".myOption option:selected").text()); 

});

1 个答案:

答案 0 :(得分:2)

试试这个(demo):

$("#createFile").click(function() {
    var values = [];

    $('#myTable .myOption').each(function(){
        values.push($(this).val());
    });

    console.log(values);
});

#myTable .myOption选择器实际上已经足够了。 .val将返回所选的选项值。