2012-04-28 72 views
1

我试图根据当前内容替换一系列'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) 

我只是无法弄清楚这个语法。

回答

1

无需使用.each() ...的.attr()方法接受一个函数作为返回新值的第二个参数被用作替换

jQuery('label[for$=new]').attr('for', function(index, currentValue){ 
    return currentValue.replace(/-new/,'-' + itemValue); 
}); 
+0

这是我追求的最佳答案正如所问。所有3个答案都是解决问题的有效方法。 – chriv 2012-04-28 16:44:30

0

不知道我得到的问题,但这样的:

var oldFor = $('label[for$=new]').attr('for'); 
var newFor = oldfor.replace(/-new/,itemValue); 

$('label[for$=new]').attr('for', newFor); 

.attr(的attributeName,value)的

attributeName = The name of the attribute to set. 
value = A value to set for the attribute. 

当选择多个元素,你需要迭代:

$('label[for$=new]').each(function(index) { 
    $(this).attr('for', $(this).attr('for').replace(/-new/, '-' + itemValue)); 
}); 
+0

我不能得到这个工作。这可能是因为我仍然不明白语法。我得到你正在创建一个为标签的每次迭代执行的函数。我没有得到的是传递给函数的参数(索引和项目)。您的示例中永远不会使用索引,并且该项是标签本身?我要查找如何在jQuery中使用.each,看看我能否弄清楚。 – chriv 2012-04-28 06:03:16

+0

终于得到了这个工作。我从函数中删除了item参数,并且在函数内部使用了这个而不是item。新代码看起来像jQuery('label [for $ = new]'。each(function(index){replacementText =' - '+ itemValue; jQuery(this).attr('for',jQuery(this).attr( “为”)代替(/ - 新的/,replacementText));}); – chriv 2012-04-28 07:13:36

+0

这个作品它并不像在jQuery的ATTR方法使用嵌套函数的答案一样高效,但它肯定工程谢谢 – chriv 2012-04-28 16:47:18

0

如果可以,为什么不把input标签放在label标签内?这样,label标记内就不需要for属性。

接下来,一个更好的方法来完成你想要做的事情是使用发票ID号作为周围div的ID,并为“新”发票条目添加一个“新”类。

所以你的形式会是这个样子:

<form id="InvoiceItemsForm-1" action="<?=$_SERVER['PHP_SELF']" method="post" name="InvoiceItemsForm-1" onsubmit="return false"> 
    <div class="InvoiceItem new"> 
     <label>New Invoice Item Number: <input class="InvoiceItemNumber" type="text" value="" name="InvoiceItemNumber"></label> 
     <label>Item Description: <input class="InvoiceItemDescription" type="text" value="" name="InvoiceItemDescription-new"></label> 
     <label for="InvoiceItemAmount-new">Item Amount: <input class="InvoiceItemAmount" type="text" value="" name="InvoiceItemAmount-new"></label> 
     <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> 

你仍然有你需要得到新的发票项目现场数据的所有靶向性,但现在,你只需要两件事情要做从“新”发票行转换为“现有”发票项行:将id属性添加到div并删除new类,这两个jQuery都会让您轻松完成。

+0

这可能是一个好主意,我仍然在学习CSS选择,并且我还没有为以上的一个类指定过元素,我也不知道在一个标签内部输入一个输入而不是在语法上是正确的,对于”属性。 – chriv 2012-04-28 06:05:23

+0

这绝对是一个好主意。简化HTML简化了的JavaScript/jQuery的,不回答我的问题作为询问,但它肯定是有帮助的。 – chriv 2012-04-28 16:45:59

+0

@chriv我想后它作为一个评论,但我还没有这个特权 – 2012-04-28 18:11:43

相关问题