卡在使用 JQuery 有条件地启用或禁用输入上
我正在尝试根据变量的值有条件地启用或禁用 HTML 输入。 “禁用”部分运行良好,但“启用”部分则不然,我不明白为什么。
代码:
<--if variable is true...-->
<div id="disabledSearch" class="searchBox disabled">
<script type="text/javascript">
$('#disabledSearch :input').attr('disabled', true);
</script>
<--if variable is false-->
<div id="enabledSearch" class="searchBox">
<script type="text/javascript">
//This line isn't working:
$('#enabledSearch :input').removeAttr('disabled');
</script>
I am attempting to conditionally enable or disable HTML input based on the value of a variable. The "disable" part is working well, but the "enable" isn't, and I can't figure out why.
The code:
<--if variable is true...-->
<div id="disabledSearch" class="searchBox disabled">
<script type="text/javascript">
$('#disabledSearch :input').attr('disabled', true);
</script>
<--if variable is false-->
<div id="enabledSearch" class="searchBox">
<script type="text/javascript">
//This line isn't working:
$('#enabledSearch :input').removeAttr('disabled');
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我玩了一下这个 - 查看 http://jsbin.com/ayoye3
input .attr('disabled',true)
和input.attr('disabled',false)
成功切换输入。唯一的可能性是您没有选择正确的元素,因此代码似乎不起作用。确保您的选择器选择正确的节点,或者在此处共享更多代码 - 您可能需要将函数放在
$(function() { ... })
包装器中,以确保它们仅DOM 加载后执行。另外,您是否遇到任何与 js 相关的浏览器错误?
I played around with this for a bit - check out http://jsbin.com/ayoye3
input.attr('disabled',true)
andinput.attr('disabled',false)
successfully toggle the inputs.The only possibility is that you're not selecting the correct elements, and so the code doesn't appear to work. Make sure your selectors are choosing the correct nodes, or share more of your code here - you might need to place your functions in a
$(function() { ... })
wrapper to ensure they only execute once the DOM is loaded.Also, are you getting any js-related browser errors?
而不是删除属性
尝试
注意:只是一个建议,但您可能应该在 $(document).ready() 函数中执行此操作。这将清理您拥有的一些重复代码。
已编辑
查看将代码迁移到 $(document).ready( function() {});并使用 $.toggle() 函数根据条件在输入上切换禁用状态。请注意,如果仅在服务器端引用该条件,您可以通过将其放置在隐藏输入中来在客户端公开该标志。
就我个人而言,假设您在 div 下确实有有效的输入,我会做类似的事情
instead of removeAttr
try
Note: just a suggestion but you should probably do this in the $(document).ready() function. this would clean up some of the duplicate code that you have.
Edited
Look at migrating the code to $(document).ready( function() {}); and using the $.toggle() function to toggle disabled on the input depending on the condition. Note if the condition is only referenced on the server side you can expose the flag on the client side by placing it in a hidden input.
Personally I would do something like this assuming you do have a valid input under the div
谢谢@pointy - 我改变了顺序,现在它可以工作了
编辑:我似乎找不到@pointy 给我这个想法的评论。然而,解决方案是我改变了页面上元素和 javascript 的顺序。
Thanks @pointy - I changed the order now it works
Edit: I can't seem to find the comment by @pointy that gave me this idea. However, the solution was that I changed around the order of the elements and javascript on the page.