我试图根据当前内容替换一系列'for'属性的标签。如何替换jQuery中一组元素的部分属性值?
该应用程序正在使用AJAX将项目添加到发票而不刷新页面。在接收到成功项目添加的通知后,我的脚本应该替换表单中'for'属性以'-new'结尾的所有标签,并使用相同的属性减去'-new'并添加(' - '+ itemValue),其中itemValue是已添加的发票项目的项目标识。
我知道如何选择所有我想一次更改标签:
jQuery('label[for$=new]')
我知道如何让自己“的”属性:
jQuery('label[for$=new]').attr('for')
我尝试了JavaScript的替代方法:
jQuery('label[for$=new]').attr('for').replace(/-new/,itemValue)
但是,似乎选择每个标签的'for'属性,替换文本,并通过r因为我不知道如何识别具有我想要替换的'for'属性的标签。
这是一些示例HTML:
<form id="InvoiceItemsForm-1" action="<?=$_SERVER['PHP_SELF'];?>" method="post" name="InvoiceItemsForm-1" onsubmit="return false">
<div id="InvoiceItem-new-1" class="InvoiceItem">
<label for="InvoiceItemNumber-new">New Invoice Item Number: </label>
<input id="InvoiceItemNumber-new" class="InvoiceItemNumber" type="text" value="" name="InvoiceItemNumber-new">
<label for="InvoiceItemDescription-new">Item Description: </label>
<input id="InvoiceItemDescription-new" class="InvoiceItemDescription" type="text" value="" name="InvoiceItemDescription-new">
<label for="InvoiceItemAmount-new">Item Amount: </label>
<input id="InvoiceItemAmount-new" class="InvoiceItemAmount" type="text" value="" name="InvoiceItemAmount-new">
<input id="addInvoiceItem-1" width="25" type="image" height="25" src="/payapp/images/greenplus.th.png" alt="Add New Invoice Item" onclick="addInvoiceItemButtonPushed(this)" value="invoiceItem">
</div>
<button id="CloseInvoice-1" onclick="closeInvoice(this)" type="button">Close Invoice</button>
</form>
一旦我得到这个工作,我要全部更换为IDS所有输入。同样的问题。我想象的解决方案看起来像这样:
jQuery('input[id$=new]').attr('id').replace(/-new/,itemValue)
我只是无法弄清楚这个语法。
这是我追求的最佳答案正如所问。所有3个答案都是解决问题的有效方法。 – chriv 2012-04-28 16:44:30