Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
178
rated 0 times [  181] [ 3]  / answers: 1 / hits: 15995  / 11 Years ago, fri, november 22, 2013, 12:00:00

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?



Code :



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

More From » html

 Answers
30

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


Edit: After viewing you HTML



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


Edit: Based on your commment



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

[#74120] Thursday, November 21, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
luzv

Total Points: 178
Total Questions: 105
Total Answers: 114

Location: Palau
Member since Tue, May 30, 2023
1 Year ago
luzv questions
Thu, Aug 26, 21, 00:00, 3 Years ago
Mon, Nov 23, 20, 00:00, 4 Years ago
Thu, Jun 11, 20, 00:00, 4 Years ago
Wed, Apr 29, 20, 00:00, 4 Years ago
;