jQuery Prev() 问题
抱歉各位,我应该直接发布脚本而不清理它。请检查更新后的脚本,现在应该可以解决问题了。谢谢!
考虑以下 JavaScript :
var selected = 0;
var options = 0;
$('.newListSelected').each(function() {
selected = $(this).children('.selectedTxt');
selected = selected.text();
/* Everything before this line works completely fine */
options = $(this).prev();
options.find('option[value=' +selected+ ']').attr('selected', 'selected');
}).remove();
和 HTML :
<select name="type[]" style="display: none;">
<optgroup>
<option value="none">Select</option>
</optgroup>
<optgroup label="First Group">
<option value="1">One</option>
<option value="2">Two</option>
</optgroup>
<optgroup label="Second Group">
<option value="10">Ten</option>
<option value="20">Twenty</option>
</optgroup>
</select>
<div class="newListSelected">
<div class="selectedTxt">20</div>
<ul class="newList">
<!-- Other stuff here -->
</ul>
</div>
我实际上想做的是将 selected
属性添加到相应的 select option 中,该属性与 < 中的文本具有相同的值代码>.selectedTxt。在上面的代码中,应将 selected="selected"
添加到 。
然而它没有按预期执行,我还尝试在 prev();
下面添加 alert(option);
但它没有输出任何有用的东西。
谢谢。
Sorry guys, I should have posted the script directly without cleaning it. Please check the updated script, it should now clear things up. Thanks!
Consider the following JavaScript :
var selected = 0;
var options = 0;
$('.newListSelected').each(function() {
selected = $(this).children('.selectedTxt');
selected = selected.text();
/* Everything before this line works completely fine */
options = $(this).prev();
options.find('option[value=' +selected+ ']').attr('selected', 'selected');
}).remove();
And HTML :
<select name="type[]" style="display: none;">
<optgroup>
<option value="none">Select</option>
</optgroup>
<optgroup label="First Group">
<option value="1">One</option>
<option value="2">Two</option>
</optgroup>
<optgroup label="Second Group">
<option value="10">Ten</option>
<option value="20">Twenty</option>
</optgroup>
</select>
<div class="newListSelected">
<div class="selectedTxt">20</div>
<ul class="newList">
<!-- Other stuff here -->
</ul>
</div>
What I'm trying to do actually is adding the selected
attribute to the corresponding select option that has the same value as the text in .selectedTxt
. In the code above, it should add selected="selected"
to <option value="20">Twenty</option>
.
However its not performing as expected, I also tried adding alert(option);
below prev();
but it didn't output anything useful.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Javascript 工作正常,如下所示: http://jsfiddle.net/4HsXp/
如果您需要能够在
select
元素中选择多个项目,您需要设置multiple
和size
属性,如下所示:参见:< a href="http://reference.sitepoint.com/html/select" rel="nofollow noreferrer">http://reference.sitepoint.com/html/select
编辑:
将
console.log
语句附加到新代码中仍然没有任何问题。 在 jsfiddle 上运行它给了我正确的输出:The Javascript works fine, as can be seen here: http://jsfiddle.net/4HsXp/
If you need to be able to select multiple items in a
select
element, you need to set themultiple
andsize
attribute, like this:See: http://reference.sitepoint.com/html/select
Edit:
Attaching
console.log
statements to the new code still does not any problem. Running it on jsfiddle gave me the correct output:我不确定您要做什么,但为什么不能使用正确的选择器选择所有选项元素?如果您只想选择某些选项,则必须添加额外的选择器属性。
I'm not sure what you are trying to do, but why couldn't you just select all option elements with proper selectors? If you want to select only some options, then you have to add extra selector attrbiutes.