-1
我有这么远我的代码,这是复制一个帐单地址,送货地址在窗体上:jQuery的变量添加到字符串
JQUERY
$("#toshipping_checkbox").on("click",function(){
var formid = $(this).closest("form").attr("id");
var idsplit = formid.split('_');
console.log(idsplit[0]);
var ship = $(this).is(":checked");
$("[id^='form1_shipping_']").each(function(){
var tmpID = this.id.split('form1_shipping_')[1];
$(this).val(ship?$("#form1_"+tmpID).val():"");
});
});
HTML
<h2>Billing Address</h2>
<div>
<label class="required" for="form1_address_1">Address 1</label>
<input id="form1_address_1" name="address_1" type="text" required="required" />
</div>
<div>
<label for="form1_address_2">Address 2</label>
<input id="form1_address_2" name="address_2" type="text" />
</div>
<div>
<label for="form1_city">City</label>
<input id="form1_city" name="city" type="text" />
</div>
<h2>Shipping Address</h2>
<label for="toshipping_checkbox">Tick this box if Shipping Address and Billing Address are the same.</label>
<input type="checkbox" id="toshipping_checkbox">
<div>
<label class="required" for="form1_shipping_address_1">Address 1</label>
<input id="form1_shipping_address_1" name="shipping_address_1" type="text" required="required" />
</div>
<div>
<label for="form1_shipping_address_2">Address 2</label>
<input id="form1_shipping_address_2" name="shipping_address_2" type="text" />
</div>
<div>
<label for="form1_shipping_city">City</label>
<input id="form1_shipping_city" name="shipping_city" type="text" />
</div>
现在我需要用idsplit[0]
替换form1
的每个实例。我已经尝试过,但一直在失败。这样做的最好方法是什么?
有关更多说明,我的CMS将form1
添加到我的输入ID,因此无法删除。另外,如果它是页面上的第二个表单,它将是form2
,所以我需要从DOM中获取该值作为变量。我已经这样做了,现在我只需要得到可变进我的jQuery的地方我有form1
目前
请提供相关html样本。演示会帮助解释此代码的用途 – charlietfl
我已经添加了html和一个解释,我希望这可以帮助 –