使用jquery“这个”;从下拉列表中返回选定的选项,但它返回页面上的所有下拉列表
我有一个页面,其中包含几乎相同的选择框列表。我希望我的函数返回已更改框中的所选值;现在,它返回页面上所有框的选定值。我认为我在某种程度上滥用了“这个”,但不知道如何使用。
<ul class="edgetoedge" id="finisherlist">
<form method="post">
<li class="entry">
<span class="finishingposition">1</span>
<span class="runnerblock">
<select placeholder="Finisher name" name="finisher"
class="finisherchoice" onChange="saveFinishOrder();">
<option value="0" selected="selected" data-skip="1">Select runner</option>
<option value="1">Bob</option>
<option value="2">Peter</option>
</select></span>
</li>
<li class="entry">
<span class="finishingposition">2</span>
<span class="runnerblock">
<select placeholder="Finisher name" name="finisher" class="finisherchoice"
onChange="saveFinishOrder();">
<option value="0" selected="selected" data-skip="1">Select runner</option>
<option value="1">Bob</option>
<option value="2">Peter</option>
</select></span>
</li>
</form>
</ul>
我单独的 JavaScript 如下所示:
function saveFinishOrder() {
alert($(this +'option:selected').text());
}
当我更改其中一个下拉列表时,我会收到类似“PeterSelectRunner”的警报 - 即它返回页面上所有下拉列表的当前选定值。我想要的是获取该下拉列表的值,以便我可以将其保存到表中。
编辑删除了上面示例代码中放错位置的标签。
I have a page with a list of almost identical select boxes. I want my function to return the selected value just in the box that's been changed; right now it returns the selected value for all boxes on the page. I think I am misusing 'this' somehow but can't figure out how.
<ul class="edgetoedge" id="finisherlist">
<form method="post">
<li class="entry">
<span class="finishingposition">1</span>
<span class="runnerblock">
<select placeholder="Finisher name" name="finisher"
class="finisherchoice" onChange="saveFinishOrder();">
<option value="0" selected="selected" data-skip="1">Select runner</option>
<option value="1">Bob</option>
<option value="2">Peter</option>
</select></span>
</li>
<li class="entry">
<span class="finishingposition">2</span>
<span class="runnerblock">
<select placeholder="Finisher name" name="finisher" class="finisherchoice"
onChange="saveFinishOrder();">
<option value="0" selected="selected" data-skip="1">Select runner</option>
<option value="1">Bob</option>
<option value="2">Peter</option>
</select></span>
</li>
</form>
</ul>
And my separate JavaScript looks like this:
function saveFinishOrder() {
alert($(this +'option:selected').text());
}
When I change one of the dropdowns I get an alert like "PeterSelectRunner" - ie it returns me the current selected values of all dropdowns on the page. What I want is to get the value of just that dropdown, so that I can then save it into a table.
EDIT deleted misplaced tags in example code above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
和
and
试试这个:
此外,检查您的 HTML,您在选项列表末尾之前有重复的结束标记。
Try this:
Besides, check your HTML, you have duplicated closing tags and before the end of options list.