我正在研究一個小腳本,並且發現了一些有重複元素的東西。在這種情況下,$('#theSelect1 ..')
被重複,我想避免它。避免在jQuery回調中重複選擇器
$('#theSelect1').change(function(){
//console.log ($(this));
console.log(
$("#theSelect1 option:selected").text() // is there a way to change #theSelect1 to be $(this) inside this statement? I try not to repeat things.
);
})
這是HTML
<select id="theSelect1">
<option value="foo" selected="selected">1</option>
<option value="bar">2</option>
<option value="foobar">3</option>
謝謝
簡短的回答 - 是的。替換$(「#theSelect1選項:選中」).text()with $(this).find('option:selected「)。text() – Derek
哇嘎快回復!謝謝! – Matt