【问题标题】:Why is my radio button not changing visually? The change fires but the visual appearance does not change为什么我的单选按钮在视觉上没有变化?更改会触发,但视觉外观不会更改
【发布时间】:2015-10-21 16:17:05
【问题描述】:

问题:单选按钮触发更改但不更改视觉外观。

我有一个简单的表单,可让您发布状态更新或新的博客文章。除文本区域外,所有表单字段在页面加载时都隐藏。当您单击文本区域时,会显示更多表单,包括两个单选按钮,可让您在状态更新或博客文章之间进行选择。两者之间的区别在于,如果您单击标准单选按钮,则博客文章会出现一个名为 Post Title 的附加字段。场景设置在这个小提琴中:

http://jsfiddle.net/XH7eP/1/

所以这就是问题所在。您会注意到,如果您单击“标准”帖子格式,帖子标题字段确实按预期显示。更改正在触发,但单选按钮未更改其视觉外观。您可以单击其中任何一个来显示/隐藏帖子标题字段,但视觉外观不会改变。

我被难住了。任何见解将不胜感激。这是小提琴的代码。

<form id="new_post_form" name="status_update" method="post" action="">
<p>Update your status</p>
<div class="upper">
    <div class="new_post_form_hiding_fields hide">
        <label>Post Format: </label>
        <input type="radio" name="new_post_format" id="status-post-format" class="post-format-radio" value="status" checked="checked" /> <label>status</label>
        <input type="radio" name="new_post_format" id="standard-post-format" class="post-format-radio" value="standard" /> <label>standard</label>
        <div class="standard_post_format_fields hide">
            <label>Post Title</label>
            <input type="text" id="new_post_title" name="title" value="" tabindex="3" />
        </div>
    </div>
</div>
<textarea name="post_content" id="new_post_content" tabindex="2" style="overflow: hidden; word-wrap: break-word; resize: horizontal; height: 47px;"></textarea>
<div class="new_post_form_hiding_fields hide">
    <input type="submit" id="new_post_submit" value="submit" tabindex="6" />
</div>
</form>

jQuery 下一个

$('#new_post_form').click(function(e) {
$('.new_post_form_hiding_fields').slideDown();
e.stopPropagation();
return false;
});
$(document).click(function() {
if( $('.new_post_form_hiding_fields' ).is(":visible") ) {
    $('.new_post_form_hiding_fields').slideUp();
}
});

$('.post-format-radio').click(function() {
if( $('#standard-post-format').prop('checked') ) {
    $('.standard_post_format_fields').fadeIn();
} else {
    $('.standard_post_format_fields').fadeOut();
}
});

【问题讨论】:

    标签: jquery forms radio-button


    【解决方案1】:

    当你使用 stopPropagation return false 表示你想停止任何点击执行,停止传播后它不需要返回任何东西,方法将负责防止事件冒泡。

     $('#new_post_form').click(function(e) {
    $('.new_post_form_hiding_fields').slideDown();
    e.stopPropagation(); });
    

    jsfiddle.net/XH7eP/3/

    【讨论】:

    • 非常感谢@Neha。我对 jQuery 还太陌生,甚至看不到这样的东西:-/ 已修复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 2019-06-11
    • 2010-09-23
    • 1970-01-01
    相关资源
    最近更新 更多