2010-06-24 51 views
1

我有一个HTML如下:如何使用jquery通过id访问类?

<fieldset id="question1"> <legend class='legend'>...</legend>

...

<input type="text" name="label_no1" id="label_no1" autocomplete="off">

</fieldset>

在Java脚本,我克隆的字段集,但我想访问其元素来改变ID和一些文字。

我尝试这样做,也没有工作:

$('#question1").siblings(".legend").html="any text"; \\ I also tried children

我也希望能够访问该字段集里面所有的投入,所以要改变它们的ID。 有什么帮助吗?

+1

这是一个有点不清楚你想要做什么。你为什么要克隆字段集 - 你想把它插入其他地方吗?你需要在克隆的字段集内改变什么? – 2010-06-24 12:31:51

+0

对于如何通过id访问类的部分,我得到了工作答案,谢谢大家对于快速回复。 然而,我仍然需要帮助如何访问在字段集上找到的输入,我是否需要为每个输入添加类,然后使用attr方法更改其ID? – Luci 2010-06-24 12:57:51

+0

是的,你必须改变输入的ID,但不是,你不需要在他们上课就可以做到这一点。我已经更新了我的答案,以说明如何做到这一点。 – 2010-06-24 13:14:25

回答

1

clone字段集并将其添加到同一个父:

var fieldset = $("#question1"); // Get the fieldset 
var clone = fieldset.clone();  // Clone it 
clone.attr("id", "question2");  // Set its ID to "question2" 
clone.appendTo(fieldset.parent()); // Add to the parent 

注意,我们changing的ID将它添加到树前,因为你不能有相同的ID两个元素。

做事情与其中的元素,你可以在你clone变量使用.children().find()有一个选择,选择你想要的孩子/后代(一旦你已经添加了克隆到父)。例如,为了清理id S于输入:

clone.find('input').each(function() { 
    if (this.id) { 
     // It has an ID, which means the original had an ID, which 
     // means your DOM tree is temporarily invalid; fix the ID 
     this.id = /* ...derive a new, unique ID here... */; 
    } 
}); 

注意,each回调中,this一个jQuery的实例,它的原始元素。 (因此,我直接设置this.id。)如果您想要为元素获取jQuery实例,则应该执行var $this = $(this);,然后改为使用$this.attr("id", ...)。但没有特别的需要,除非你改变ID以外的事情。


回答你的问题有关重编的ID,你需要确保你使用更新的的ID以及实际的ID输入元素无论是

但在这样做的更新上投入要素方面,你可以通过阅读数量和递增它,直到你得到一个未使用的做到这一点:

clone.find('input').each(function() { 
    var id; 
    if (this.id) { 
     id = this.id; 
     do { 
      id = id.replace(/[0-9]+$/g, function(num) { 
       return String(Number(num) + 1); 
      }); 
     } 
     while ($("#" + id).length > 0); 
     this.id = id; 
    } 
}); 

...这会给你“input_radio2”,如果原始ID是“input_radio1”,但我想我可能会使用不同的命名约定。举例来说,你可以用这个问题的ID前缀您的各种输入标识:

<fieldset id='question1'> 
    ... 
    <input id=-'question1:input_radio1' /> 
</fieldset> 

...然后只需更换“问题1”与“问题2”在克隆标识。 (Colons [:]在ID中是完全有效的。)

但是,如果您可以避免在您的所有输入上都有ID,那就是要走的路。举例来说,除非你的标记防止其出于不同的原因,你可以通过遏制其labelinput而不是使用for关联:

<label>First name: <input type='text' ... /></label> 

很多的选择。 :-)

+0

@zeina:大声笑,我只是回答你之前提出的问题,我的解决方案是类似的,但是防止冲突,但我认为这可能是值得退一步并重构形式的一点点(他说,不知道有什么需求!):-) – 2010-06-24 14:07:34

+0

非常感谢。最后一个问题,如果我想删除克隆,我该如何做到这一点?只需将其设置为隐藏?我怀疑是否有更好的方法。 – Luci 2010-06-24 14:22:04

+0

@zeina:你的意思是,如果你想在某个时候删除克隆,只需调用'$(“#idOfClone”)。remove()':http://api.jquery.com/remove/ – 2010-06-24 14:23:59

0

jQuery的html方法有一个语法如下:

var result = $(selector).html(); // result now contains the html of the first matched element 

@(selector).html('some html markup'); // the html of all matched elements are set to 'some html markup' 
2

您可以使用attr方法改变问题的ID:

var q2 = $('#question1').clone(); 
q2.attr('id', 'question2'); 

若要在新编辑特定儿童元素,你想要find方法:

var legend = q2.find('.legend').html('....'); 
+0

谢谢德克斯特,你的方法奏效了。现在,如果我想更改字段集中输入的ID,您知道该怎么做吗?请注意,我内部有很多输入。 – Luci 2010-06-24 12:38:22

+0

有一个错字:'$('#question1“)。clone();'应该是'$('#question1')。clone();''''你用双引号关闭了一个带引号的字符串 – jigfox 2010-06-24 12:44:57

+0

Thanks @Jens - 固定的 – Dexter 2010-06-24 12:54:25

2

试试这个:

$clone = $('#question1').clone(); 

$clone.attr('id','some_new_id'); 
// you don't need to search by class or id you can search for tags also, 
// so you can get rid of some redundancy and write <legend>…</legend> 
// instead of <legen class="legend">…</legend> 
// siblings won't work with your example, because legend is a child not a sibling 
// html= won't work either html on a jQuery object is a method not an attribute 
$clone.find('legend').html("New text for the Legend"); 
+1

@zeina:如果内容字段集有'id's,但是* *需要清除它们,除非它们被替换,因为它们在'legend'元素中。你的问题中的HTML代码片段在'legend'元素之外(他们有'id's),所以这不会完成它,尽管它会让你接近。 – 2010-06-24 13:09:18