隐藏/显示表单输入和更改值

时间:2014-09-11 14:45:14

标签: javascript jquery forms validation twitter-bootstrap-3

我很难过,所以现在我转向我的同事们寻求帮助。这可能是我忽略的非常简单的事情,但你知道当你对某些事情看起来过于苛刻时会是怎样的,通常情况就是这样,你就会错过它。任何方式漫无目的地漫无目的!到了这一点!

我有这个应用程序,我只需要帮助它的片段。它已经与bootstrap 3放在一起,所以我不知道这是否与它有关。我想要做的是,点击复选框#mailing_address,检查更改,如果选中,则添加当前地址部分的值#p_address,#p_address2,#p_city,#p_state,#p_zip ,在下一组输入中各自的#m_值,然后禁用#m_输入,以便它们不能被更改。这部分工作

如果未选中该框,则再次更改#mailing_address,清除所有#m_输入并再次启用输入。这是不起作用的部分。

以下是给我这个问题的表单摘录

<div class='form-group'>
                <div class='col-md-5 col-md-offset-1 col-sm-6'>
                    <div class='input-group app-input'>
                        <input type='text' class='form-control' id='p_address' placeholder='Your Current Address'>
                        <span class='input-group-addon'><span class='glyphicon glyphicon-home'></span></span>
                    </div>
                </div>
            </div>
            <div class='form-group app-input-no-group'>
                <div class='col-md-5 col-md-offset-1 col-sm-6'>
                        <input type='text' class='form-control' id='p_address2' placeholder='Current Address Line 2'>
                </div>
            </div>
            <div class='form-group app-input-no-group'>
                <div class='col-md-3 col-md-offset-1 col-sm-4'>
                    <input type='text' class='form-control' id='p_city' placeholder='Your Current City'>                        
                </div>
            </div>
            <div class='form-group app-input-no-group'>
                <div class='col-md-3 col-md-offset-1 col-sm-4'>
                    <select class='form-control' id='p_state'>                      
                        <option value="" selected>Your Current State</option>
                        <? include $up_level.'inc/body/extras/state_list_options.php';?>
                    </select>
                </div>
            </div>
            <div class='form-group app-input-no-group'>
                <div class='col-md-3 col-md-offset-1 col-sm-4'>
                    <input type='text' class='form-control' id='p_zip' placeholder='Your Current Zip'>                      
                </div>
            </div>
            <div class='form-group'>
                <div class='col-md-5 col-md-offset-1'>
                    <div class='checkbox'>
                        <label><input type='checkbox' id='mailing_address'>Check here if your mailing address is the same as your current address</label>
                    </div>
                </div>
            </div>
            <div class='form-group'>
                <div class='col-md-5 col-md-offset-1 col-sm-6'>
                    <div class='input-group app-input'>
                        <input type='text' class='form-control' id='m_address' placeholder='Your Mailing Address'>
                        <span class='input-group-addon'><span class='glyphicon glyphicon-home'></span></span>
                    </div>
                </div>
            </div>
            <div class='form-group app-input-no-group'>
                <div class='col-md-5 col-md-offset-1 col-sm-6'>
                        <input type='text' class='form-control' id='m_address2' placeholder='Mailing Address Line 2'>
                </div>
            </div>
            <div class='form-group app-input-no-group'>
                <div class='col-md-3 col-md-offset-1 col-sm-4'>
                    <input type='text' class='form-control' id='m_city' placeholder='Your Mailing City'>                        
                </div>
            </div>
            <div class='form-group app-input-no-group'>
                <div class='col-md-3 col-md-offset-1 col-sm-4'>
                    <select class='form-control' id='m_state'>                      
                        <option value="" selected>Your Mailing State</option>
                        <? include $up_level.'inc/body/extras/state_list_options.php';?>
                    </select>
                </div>
            </div>
            <div class='form-group app-input-no-group'>
                <div class='col-md-3 col-md-offset-1 col-sm-4'>
                    <input type='text' class='form-control' id='m_zip' placeholder='Your Mailing Zip'>                      
                </div>
            </div>

用于引用包含文件&quot; state_list_options.php&#39;的include语句只是一个包含状态名称和缩写的选项列表,如下所示:

<option value='AZ'>Arizona</option>

这是我为其命名的Jquery代码

$(document).ready(function(){
    //check if the mailing address checkbox is checked
    //if so then we will fill the mailing address in with the information from current address   then hide all the mailing address fields
    //else clear the mailing address fields and show them again
    //set variables 
    var p_values = ['p_address', 'p_address2', 'p_city', 'p_state', 'p_zip'];
    var m_values = ['m_address', 'm_address2', 'm_city', 'm_state', 'm_zip'];
    $("#mailing_address").change(function(){
        if($(this.checked)){
        //if checked get the values from the current address fields
            for(var i = 0; i < p_values.length; i++){
                $('#'+m_values[i]+'').val($('#'+p_values[i]+'').val());
                $('#'+m_values[i]+'').attr('disabled', 'disabled');
            }
        }
        else{
            //this is the part that is failing
            for(var i = 0; i < p_values.length; i++){
                $('#'+m_values[i]+'').val("");
                $('#'+m_values[i]+'').removeAttr('disabled');
            }
        }
    });
});

是否与我使用id而不是输入的类或名称这一事实有关?我认为这不重要。我真的不知道,我绝对难过。如果需要,我可以添加更多细节让我知道!

2 个答案:

答案 0 :(得分:2)

$() jsFiddle

周围移除this.checked
if(this.checked){

如果你想让它成为一个jQuery对象,那么它就像这样

if($(this).is(':checked')){

答案 1 :(得分:1)

使用if($(this).is(':checked')){ - 检查jQuery的正确语法。

http://jsfiddle.net/cfm1v0p7/