获取最接近的选定选项

Dav*_*vid 0 jquery select option

我无法在多个表单(带有多个提交按钮)中的提交按钮附近找到最接近的选定选项(文本)。但我不知道如何解决这个问题?

代码 :

$(document).ready(function() {
    $("form").submit(function() {
        var x = $(this).closest('form').find('input[type=submit]');
        var y = $(x).closest('select option:selected').text();
        alert(y);
        });
    });

<form action="<?php echo site_url('manage/deleteVendor'); ?>" method="POST">
    <table cellspacing='10'>
        <tr>
            <td>Delete Vendor</td>
        </tr>
        <tr>
        <td>Vendor</td>
        <td><select name="vendor" class="vendor">
            <?php foreach ($vendors as $vendor) { ?>
            <option value="<?php echo $vendor->ID; ?>" ><?php echo $vendor->NAME; ?></option>
            <?php } ?>
        </select></td>
        <td><input type="submit" name ="submit" value="Delete Vendor"/></td>
        </tr>
    </table>
</form>

<form action="<?php echo site_url('manage/deleteVendor'); ?>" method="POST">
    <table cellspacing='10'>
        <tr>
            <td>Delete Vendor 2</td>
        </tr>
        <tr>
            <td>Vendor</td>
            <td><select name="vendor" class="vendor">
                <?php foreach ($vendors as $vendor) { ?>
                <option value="<?php echo $vendor->ID; ?>" ><?php echo $vendor->NAME; ?></option>
                <?php } ?>
                </select>
            </td>
            <td><input type="submit" name ="submit" value="Delete Vendor"/></td>
        </tr>
    </table>
</form>             
Run Code Online (Sandbox Code Playgroud)

wri*_*wan 6

找到最接近的selectoption在其中找到选定的,然后.html()获取文本。

var y=$('selector').closest('select').find(':selected').html(); //find the text
var x=$('selector').closest('select').find(':selected').val(); //find the value

alert(y);
alert(x);
Run Code Online (Sandbox Code Playgroud)

编辑:查看您的 HTML 后

var SelectedOptionText=$('.vendor').find(':selected').html();
alert(SelectedOptionText);
Run Code Online (Sandbox Code Playgroud)

编辑:根据您的评论

$(document).ready(function() {
    $("form").submit(function() {
         var x =$(this).find('.vendor :selected').html();
         alert(x);
    });
});
Run Code Online (Sandbox Code Playgroud)