jQuery 切换首先传递 boolean false 有不同的转换
这是一个工作示例: http://jsfiddle.net/FzZwt/8/
如果我传递 false首先将切换设置为慢速 如果我首先将 true 传递给toggle(),则未设置转换
我可以在toggle() 中传递带有布尔值的转换吗?
示例:
var show = true;
$('#elem').toggle(show, 'slow');
HTML
<div data-role="page">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-type="horizontal">
<div>Boolean Test 1 - Passing TRUE first</div>
<input type="radio" name="boolean" id="true" value="true" />
<label for="true">Yes</label>
<input type="radio" name="boolean" id="false" value="false" checked="checked"/>
<label for="false">No</label>
<div class="hidden" name="hidden_div" id="hidden_div">Show Me!!!</div>
</fieldset>
</div>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-type="horizontal">
<div>Boolean Test 2 - Passing FALSE First</div>
<input type="radio" name="boolean2" id="true" value="true" />
<label for="true">Yes</label>
<input type="radio" name="boolean2" id="false" value="false" checked="checked"/>
<label for="false">No</label>
<div class="hidden" name="hidden_div2" id="hidden_div2">Show Me!!!</div>
</fieldset>
</div>
</div>
JS
$(".hidden").hide();
$("[name=boolean]").change(function() {
var show = $('input[name=boolean]:checked').val() == "true";
alert('Passing first: '+show);
$("#hidden_div").toggle(show);
});
$("[name=boolean2]").change(function() {
var show = $(this).val();
alert('Passing First: '+show);
$("#hidden_div2").toggle(show);
});
Here is a working example: http://jsfiddle.net/FzZwt/8/
If I pass false to the toggle() first the transition is set to slow
If I pass true to the toggle() first the transition is not set
Can I pass a transition with a boolean in the toggle()?
Example:
var show = true;
$('#elem').toggle(show, 'slow');
HTML
<div data-role="page">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-type="horizontal">
<div>Boolean Test 1 - Passing TRUE first</div>
<input type="radio" name="boolean" id="true" value="true" />
<label for="true">Yes</label>
<input type="radio" name="boolean" id="false" value="false" checked="checked"/>
<label for="false">No</label>
<div class="hidden" name="hidden_div" id="hidden_div">Show Me!!!</div>
</fieldset>
</div>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-type="horizontal">
<div>Boolean Test 2 - Passing FALSE First</div>
<input type="radio" name="boolean2" id="true" value="true" />
<label for="true">Yes</label>
<input type="radio" name="boolean2" id="false" value="false" checked="checked"/>
<label for="false">No</label>
<div class="hidden" name="hidden_div2" id="hidden_div2">Show Me!!!</div>
</fieldset>
</div>
</div>
JS
$(".hidden").hide();
$("[name=boolean]").change(function() {
var show = $('input[name=boolean]:checked').val() == "true";
alert('Passing first: '+show);
$("#hidden_div").toggle(show);
});
$("[name=boolean2]").change(function() {
var show = $(this).val();
alert('Passing First: '+show);
$("#hidden_div2").toggle(show);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当它需要布尔值或动画速度(数字或转换为数字的值之一)时,您正在传递字符串值。
如果你想传递布尔值,你需要将它们转换为 Tomalak 提到的
我做了一些更改;这是您要找的吗?
http://jsfiddle.net/FzZwt/11/
You're passing string values when it expects either a boolean or an animation speed (either a number or one of the values that translates to numbers).
If you want to pass booleans, you'll need to cast them as Tomalak mentions
I made a couple of changes; is this what you're looking for?
http://jsfiddle.net/FzZwt/11/
根据API,
toggle()
的形式其中布尔值确定是否显示或隐藏不采用任何其他参数。不幸的是,我认为我们必须做类似的事情:不是很优雅,但我认为这是必要的,因为
toggle
函数的重载方式。According to the API, the form of
toggle()
where a boolean determines whether to show or hide doesn't take any other parameters. So unfortunately, I think we have to do something like:Not very elegant, but I think this is necessary because of the way the
toggle
function is overloaded.