JQuery - 从下拉列表中选择的选项中删除复选框
我知道这个标题有点过分。
我想要的是我有一个下拉菜单,里面有一些选项,其中两个选项是“特殊的”。当你选择这两个之一时,3个复选框应该被灰色/删除/等..
这里是一些代码:
HTML:
//THE DROPDOWN
<select id="pow-abo" style="float:left;">
<option id="data6" value="149">XXXX</option>
<option id="data11" value="199">XXXX</option>
</select>
//THE CHECKBOXES
<div id="pakkerPow">
<input id="surfstart" type="checkbox" value="29" />XXXX<br/>
<input id="surfmini" type="checkbox" value="49" />XXXX<br/>
<input id="surfmaxi" type="checkbox" value="99" />XXXX<br/>
</div>
Jquery:
// THE JQUERY DROPDOWN
$('#pow-abo').change(function (){
powAbo = parseFloat($('#pow-abo').val());
if($('#pow-abo').id() == 'data6' || $('#pow-abo').id() == 'data11')
{
$('#surfstart').hide();
$('#surfmini').hide();
$('#surfmaxi').hide();
}
visResultat2();
});
i know the title is a bit wired.
what i want is that i have a dropdown, theres some options in there, two of those options are "special". When you choose one of those two, 3 checkboxes should be gryed out/removed/ect..
heres some code:
HTML:
//THE DROPDOWN
<select id="pow-abo" style="float:left;">
<option id="data6" value="149">XXXX</option>
<option id="data11" value="199">XXXX</option>
</select>
//THE CHECKBOXES
<div id="pakkerPow">
<input id="surfstart" type="checkbox" value="29" />XXXX<br/>
<input id="surfmini" type="checkbox" value="49" />XXXX<br/>
<input id="surfmaxi" type="checkbox" value="99" />XXXX<br/>
</div>
Jquery:
// THE JQUERY DROPDOWN
$('#pow-abo').change(function (){
powAbo = parseFloat($('#pow-abo').val());
if($('#pow-abo').id() == 'data6' || $('#pow-abo').id() == 'data11')
{
$('#surfstart').hide();
$('#surfmini').hide();
$('#surfmaxi').hide();
}
visResultat2();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的代码存在一些问题。它们以您的
if
语句为中心。看下面的代码片段:第一个问题是
id
不是 jQuery 对象的方法。第二个问题是#pow-abo
是select
元素,而不是选定的选项。您似乎正在尝试获取所选选项的id
。要获取选定的选项,作为 DOM 元素(而不是 jQuery 对象),您可以执行以下操作:
然后您可以使用
selected.id
来获取id
所选选项:请注意,您还可以将
hide
调用缩短为一个:或者,隐藏
#pakkerPow
内的所有复选框:There are a couple of problems with your code. They centre around your
if
statement. Look at the following snippet:The first problem is that
id
is not a method of a jQuery object. The second problem is that#pow-abo
is theselect
element, not the selected option. It looks like you are trying to get theid
of the selected option.To get the selected option, as a DOM element (not a jQuery object) you could do something like this:
You would then be able to use
selected.id
to get theid
of the selected option:Note that you could also shorten the
hide
calls into one:Or, to hide all the checkboxes inside
#pakkerPow
:我会这样做: http://jsfiddle.net/pZcAF/6/
简而言之:
隐藏()
复选框而不是禁用它们show()
)它们中的附加功能示例(作为奖励,如果您发现它有用):
I would do it this way: http://jsfiddle.net/pZcAF/6/
In short:
hide()
the checkboxes instead of disabling themshow()
) them backAdditional feature in the example (as a bonus, in case you find it useful):
为了“变灰”或禁用表单元素,您必须向该元素添加禁用属性。
而不是
执行此操作
启用禁用的元素
$('#surfstart').removeAttr('disabled')
;In order to 'grey out' or disable a form element, you have to add the disabled attribute to the element.
Instead of
Do this
To enable a disabled element
$('#surfstart').removeAttr('disabled')
;如果你真的想使用显示和隐藏,请看一下这个演示:
http://jsfiddle.net/xAcz7/
我不是看 id,而是看值,这是隐藏选项 1 和 2,显示选项 3。
我没有隐藏 3 个复选框,而是隐藏了包含复选框的 div。节省您的代码行;)
希望这有帮助
if you really want to use show and hide take a look at this demo:
http://jsfiddle.net/xAcz7/
Instead of looking at the id im looking at the values, which is hide for options 1 and 2 and show for option 3.
And instead of hiding the 3 checkboxes im just hiding the div that contains the checkboxes. saves you lines of code ;)
Hope this helps
您的测试应该针对选择值,而不是针对 ids:
Your test should be on the select value, not on ids: