更改选择选项后,未启用输入字段

时间:2014-01-22 12:22:21

标签: javascript jquery html

我创建了一个独立的代码来启用/禁用输入字段,它运行正常。 HTML:

Identification Type:
    <select name="Identification-Type" id="Identification-Type">
        <label for="Identification-Type">Identification Type:</label>
        <option value="1111">--Select--</option>
         <option value="23434">--sfgdg--</option>
         <option value="135111">--dfgb--</option>
         <option value="1165611">--gdg--</option>
         <option value="114511">--vcbc--</option>

    </select>   
            <!-- <input type="checkbox" class="Identification-Number" value="Identification-Number" 
            name="Identification-number" id="Identification-Number"> -->
            <label  for="Identification-Number"><em>*</em>Identification Number:</label>
                <input type="text" name="Identification-Number" id="Identification-Number">

JS:

  $('select[name="Identification-Type"]').change(function () {

              var $this = $('#Identification-Number');


                 $this.attr("disabled", false);
            $this.attr("disabled", ($(this).val() == '1111') ? true : false);

            }).trigger('change');

JSFIDDLE LINK

但是,当我试图将这种逻辑合并到另一种形式时,它无法正常工作。

HTML:

        <form name="pancettaForm" method="post" action="demor" id="pancettaForm">
    <ul>
        <li>
            <label for="PartyChoose">Choose Appropriate Party:</label>
        </li>
        <br>
        <input id="person" name="PartyChoose" type="radio" value="update-person" class="required" />Person
        <br />
        <input id="organization" name="PartyChoose" type="radio" value="update-organization" class="required" />Organization
        <br />
        <li id="Family-Name" style="display: none;">
            <input type="checkbox" class="Family-Name" value="Family-name" name="Family-name">
            <label for="Family-Name"><em>*</em>Family Name:</label>
            <input type="text" name="Family-Name" class="required">
        </li>
        <li id="Organization-Name" style="display: none;">
            <inpname="Organization-name">
                <label for="Organization-Name"><em>*</em>Organization Name:</label>
                <input type="text" name="Organization-Name" class="required">
        </li>
        <div class="extraPersonTemplate">
            <div class="controls-row">
                <li id="Identification-Type" style="display: none;">Identification Type:
                    <select name="Identification-Type" class="Identification-Type">
                        <label for="Identification-Type">Identification Type:</label>
                        <option value="1111">--Select--</option>
                        <option value="1">--sdsd--</option>
                        <option value="2">--cxc--</option>
                        <option value="3">--cvcv--</option>
                        <select>    <a id="Identification-Number" style="display: none;">
                  <input type="hidden" class="Identification-Number">                                
                                <label for="Identification-Number"><em>*</em>Identification Number:</label>
                                <input type="text" name="Identification-Number">
                                    </a>

                </li>
            </div>
        </div>
        <div id="container"></div>
<a href="#" id="addRow" style="display: none;"><i class="icon-plus-sign icon-white">
</i> Add Identifier</a>

        <li id="Adminsys-Type" style="display: none;">Admin System Type:
            <select name="Adminsys-Type" class="Adminsys-Type">
                <label for="Adminsys-Type">Admin Type:</label>
                <option value="0">--Select--</option>
            </select>
        </li>
        <li id="Adminsys-Number" style="display: none;">
            <input type="checkbox" class="Adminsys-Number" value="Adminsys-Number" name="Adminsys-number">
            <label for="Adminsys-Number"><em>*</em>Admin System Value:</label>
            <input type="text" name=Adminsys-Number>
        </li>
    </ul>
    <input type="submit" id="button" name="submit" value="Search">
</form>

JS:

        $(document).ready(function () {

    var counter = 0;



    $('input[name=Organization-Name]').attr('disabled', true);
    $('input[name=Identification-Number]').attr('disabled', true);
    $('input[name=Family-Name]').attr('disabled', true);
    $('input[name=Adminsys-Number]').attr('disabled', true);

    $('#pancettaForm').change(function () {


        $('.Organization-Name').click(function () {
            if ($('.Organization-Name').is(':checked')) {
                $('input[name=Organization-Name]').val('').attr('disabled', false);
            } else {
                $('input[name=Organization-Name]').attr('disabled', true);
            }

        });

        $('select[name="Identification-Type' + counter + '"]').change(function () {

            var $this = $('.Identification-Number');
            var $input = $this.siblings('input[type=text]');
            $input.attr("disabled", false);
            $input.attr("disabled", ($(this).val() == '1111') ? true : false);
        });


        $('.Adminsys-Number').click(function () {
            if ($('.Adminsys-Number').is(':checked')) {
                $('input[name=Adminsys-Number]').val('').attr('disabled', false);
            } else {
                $('input[name=Adminsys-Number]').attr('disabled', true);
            }

        });
        $('.Family-Name').click(function () {
            if ($('.Family-Name').is(':checked')) {
                $('input[name=Family-Name]').val('').attr('disabled', false);
            } else {
                $('input[name=Family-Name]').attr('disabled', true);
            }

        });
        $('#Family-Name,#Identification-Number,#Organization-Name').hide();
        if ($('#person').prop('checked')) {
            $('#Family-Name,#Identification-Type,#Identification-Number,#Adminsys-Number,#Adminsys-Type,#addRow,#removeRow').show();


        } else if ($('#organization').prop('checked')) {
            $('#Organization-Name,#Identification-Type,#Identification-Number,#Adminsys-Number,#Adminsys-Type,#addRow,#removeRow').show();

        }
    });
    $('<div/>', {
        'class': 'extraPerson',
        html: GetHtml()
    }).appendTo('#container');




    $('#addRow').click(function () {
        if (counter > 10) {
            alert("Only 10 textboxes allow");
            return false;
        }



        $('<div/>', {
            'class': 'extraPerson' + counter,
                'id': 'extraPerson' + counter,
            html: GetHtml() + '<a href="javascript:void(0);" class="removeRow" ><i class="icon-plus-sign icon-white"></i> Remove Identifier</a>'
        }).hide().appendTo('#container').slideDown('slow');



        counter++;


    });



    $("#container").on('click', '.removeRow', function () {
        //$("#extraPerson"+counter).remove();
        if (counter < 1) {
            alert("No more textbox to remove");
            return false;
        }
        counter--;
        $(this).parent().remove();


    });

    function GetHtml() {
        // var len = $('.extraPerson').length;

        var $html = $('.extraPersonTemplate').clone();
        if (counter == 0) {
            $html.find('[name=Identification-Number]')[0].name = "Identification-Number" + counter;
            $html.find('[id=Identification-Number]')[0].name = "Identification-Number" + counter;
            $html.find('[name=Identification-Type]')[0].name = "Identification-Type" + counter;

            counter++;
            return $html.html();

        } else {
            $html.find('[name=Identification-Number]')[0].name = "Identification-Number" + counter;
            $html.find('[id=Identification-Number]')[0].name = "Identification-Number" + counter;
            $html.find('[name=Identification-Type]')[0].name = "Identification-Type" + counter;

            // $html.find('[id=Identification-Type]')[0].id="Identification-Type" + counter;

            // var remove='<a href="javascript:void(0);" class="removeRow" ><i class="icon-plus-sign icon-white"></i> Remove Identifier</a>';
            return $html.html();
        }
    }
})

JSFIDDLE LINK

如何动态更改select属性的名称,以便我可以有选择地启用和禁用多行输入字段。

2 个答案:

答案 0 :(得分:0)

希望这会对你有所帮助,我希望我说得对:

我重新设定了更改功能,该功能决定了选择框并启用输入字段,如此

$('.Identification-Type').change(function () {
    //#Identification-Type input
    /** this can be used to count the input fields and use it in a loop later **/
    var $inputFields = $('.extraPersonTemplate #Identification-Type input').length;

    var $idNumber = $('input[name=Identification-Number]');
    var $idNumber0 = $('input[name=Identification-Number0]');
    ($('select[name=Identification-Type]').val() == '1111') ? $idNumber.attr('disabled', true) :  $idNumber.removeAttr('disabled');
    ($('select[name=Identification-Type0]').val() == '1111') ? $idNumber0.attr('disabled', true) :  $idNumber0.removeAttr('disabled')
})

但是从我的观点来看,这不是最好的方法,因为它不是非常动态。 如果您设法计算select[name=Identification-Type] + counter不仅仅针对一个输入,而且针对<input type="text" name="Identification-Number0"><input type="text" name="Identification-Number1">这两个输入,可以在此更改函数中包含一个循环并循环找到的$inputFields

答案 1 :(得分:0)

http://jsfiddle.net/xKL44/2/过去这对我有所帮助......试试吧!

$('#wow').change(function() {
// Remove any previously set values
$('#show_box, #total_box').empty();
var sum = 0,
    price;
$(this).find('option:selected').each(function() {
    // Check that the attribute exist, so that any unset values won't bother
    if ($(this).attr('data-price')) {            
        price = $(this).data('price');
        sum += price;
 $('#show_box').append('<h6>' + price + '</h6>');
      }
    });
 $('#total_box').text(sum);

});