使用jquery以另一种形式使用数据属性填充1个表单数据

时间:2015-08-20 08:04:12

标签: php jquery forms laravel-5

在我的laravel 5电子商务网络应用程序中,如果选中shipping_address的{​​{1}},我会尝试填充与billing_address表单值相同的checkbox表单值。< / p>

我正在尝试使用name = same_as_billing属性执行此操作,但我未能实现目标。

我想要的是:data-*表单值应使用billing_address属性自动填充shipping_address表单值。

此处的格式为data-values

billing_address

此处的格式为<div class="form-group"> {!! Form::label('address_1', 'Address 1:') !!} {!! Form::text('address_1', $billing->address_1, ['class' => 'form-control input-sm', 'data-values' => $billing->address_1]) !!} </div> <div class="form-group"> {!! Form::label('address_2', 'Address 2:') !!} {!! Form::text('address_2', $billing->address_2, ['class' => 'form-control input-sm', 'data-values' => $billing->address_2]) !!} </div> <div class="form-group"> {!! Form::label('area', 'Area:') !!} {!! Form::text('area', $billing->area, ['class' => 'form-control input-sm', 'data-values' => $billing->area]) !!} </div> <!-- Rest of the form values -->

shipping_address

我尝试过的jquery代码:

<div class="form-group">
    {!! Form::checkbox('same_as_billing', 'same_as_billing', false, ['id' => 'same_as_billing']) !!}
    <label for="same_as_billing">Shipping Address Same As Billing Address</label>
</div>

<div class='shippingAddressFormFields'>
    <div class="form-group">
        {!! Form::label('address_1', 'Address 1:') !!}
        {!! Form::text('address_1', $billing->address_1, ['class' => 'form-control input-sm']) !!}
    </div>

    <div class="form-group">
        {!! Form::label('address_2', 'Address 2:') !!}
        {!! Form::text('address_2', $billing->address_2, ['class' => 'form-control input-sm']) !!}
    </div>

    <div class="form-group">
        {!! Form::label('area', 'Area:') !!}
        {!! Form::text('area', $billing->area, ['class' => 'form-control input-sm']) !!}
    </div>
</div>

    <!-- Rest of the form values -->

var inputField = $('.shippingAddressFormFields').find('input'); inputField.val(''); var selectField = $('.shippingAddressFormFields').find('select'); selectField.val(''); $('#same_as_billing').on('click', function() { if( $(this).is(':checked') ) { inputField.prop('readonly', true); selectField.prop('disabled', true); $("[data-values]").filter(function() { return $(this).data('values'); }).each(function(e, v) { v.value = $("[data-values]").data('values'); console.log(e + ": " + v.value); }); } else { inputField.prop('readonly', false); selectField.prop('disabled', false); inputField.val(''); selectField.val(''); } }); 的输出错误:

console.log(e + ": " + v.value);

0: BAK Building, This Colony // <-- address_1 - correct 1: BAK Building, This Colony // <-- address_2 - incorrect 所需输出应为:

console.log(e + ": " + v.value);

所需的输出应填满0: BAK Building, This Colony // <-- address_1 - correct 1: That Road, Some Landmark // <-- address_2 - correct 表单值。

我怎样才能做到这一点?

感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:1)

您从use_their.sh错误地访问了该值,您当前的代码只会返回我认为的第一个$.each,这就是为什么它无效。

请尝试data-values

然后您可以将值绑定为:

console.log(v.dataset.values);

见工作小提琴:http://jsfiddle.net/ntp3Lqpj/2/