Lütfen, jQuery kullanarak bir HTML seçme açılan kutusunun seçilen anahtarını ve değerini nasıl alabilirim?
$(this).find("select").each(function () {
if ($.trim($(this).val()) != '') {
searchString += $.trim($(this).val()) + " "; //This gives me the key. How can I get the value also?
}
});
Teşekkürler
Sanırım "key" ve "value" ile şunu kastediyorsunuz:
<select>
<option value="KEY">VALUE</option>
</select>
Eğer durum buysa, bu size "DEĞER" kazandıracaktır:
$(this).find('option:selected').text();
Ve bu şekilde "KEY" alabilirsiniz:
$(this).find('option:selected').val();
Bu işe yarıyor:
<select name="foo" id="foo">
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
</select>
<input type="button" id="button" value="Button" />
$('#button').click(function() {
alert($('#foo option:selected').text());
alert($('#foo option:selected').val());
});