我正在尝试以动态形式将_1,_2等添加到输入元素的末尾。用户可以添加他们想要的多少我写这个方法,但它似乎抛出错误说“dName未定义”
$j("#addBranch").click(
function()
{
//see withDataAndEvents, copies events with bool in clone method
$j('.branchOffice').first().clone(true).appendTo('#branchOffice');
$j('.branchOffice').last().children().attr('name',function()
{
var dName = $j(this.name).attr('name');
return dName.substring(0, dName.length-1) + ($j('.branchOffice').size() + 1)
});
}
);
这是HTML
<div class="leftPane">
<label for="company_name">Company Name</label>
<input class="req" type="text" name="company_name" id="company_name" />
<label for="branch_office">Home Office</label>
<input class="req" type="text" name="branch_office_1" id="branch_office_1" />
<label for="branch_office_add1_1">Address</label>
<input class="req" type="text" name="branch_office_add1_1" id="branch_office_add1_1" />
<label for="branch_office_add2_1">Address 2</label>
<input type="text" name="branch_office_add2_1" id="branch_office_add2_1" />
<label for="branch_office_city_1">City</label>
<input class="req" type="text" name="branch_office_city_1" id="branch_office_city_1" />
<label for="branch_office_state_1">State</label>
<select id="branch_office_state_1" name="branch_office_state_1">
<?php echo $formHelper->stateList; ?>
</select>
<label class="naked" for="branch_office_zip_1">Zip</label>
<input class="req zip" type="text" name="branch_office_zip_1" id="branch_office_zip_1" />
</div>
<div class="rightPane" id="branchOffice">
<div class="branchOffice">
<label for="branch_office_2">Branch Office</label>
<input class="req" type="text" name="branch_office_2" id="branch_office_2" />
<label for="branch_office_add1_2">Address</label>
<input class="req" type="text" name="branch_office_add1_2" id="branch_office_add1_2" />
<label for="branch_office_add2_2">Address 2</label>
<input type="text" name="branch_office_add2_2" id="branch_office_add2_2" />
<label for="branch_office_city_2">City</label>
<input class="req" type="text" name="branch_office_city_2" id="branch_office_city_2" />
<label for="branch_office_state_2">State</label>
<select id="branch_office_state_2" name="branch_office_state_2">
<?php echo $formHelper->stateList; ?>
</select>
<label for="branch_office_zip_2">Zip</label>
<input class="req zip" type="text" name="branch_office_zip_2" id="branch_office_zip_2" />
</div>
</div>
<div class="spacer"></div>
<small class="red">If more than one branch office, please</small><a class="addMore" title="Add Branch" id="addBranch">Add More</a>
答案 0 :(得分:0)
var dName = $j(this).attr('name');
或者,现在我再看一遍,$j(this)
是指.branhOffice
而不是#addBranch
。
也许设置一个val;
var addBranch = $(this);
然后你的另一个选择
var dName = $(addBranch).attr('name');