获得最接近的选定选项

获得最接近的选定选项

本文介绍了获得最接近的选定选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法以多种形式(带有多个提交按钮)在提交按钮附近找到最接近的选定选项(文本).但是我什么也没办法解决?

I am having trouble finding the closest selected option (text) near a submit button in multiple forms (with multiple submit buttons).But I don't get anything how to fix this?

代码:

$(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>

推荐答案

找到最接近的select并在其中找到所选的option,然后在.html()处获取文本.

Find the closest select and find the selected option in it followed by .html() to get the text.

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

查看HTML后

var SelectedOptionText=$('.vendor').find(':selected').html();
alert(SelectedOptionText);

根据您的评论

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

这篇关于获得最接近的选定选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 23:36