[英]How to get ID value of first element with specific class in html by jQuery
I have this HTML code: 我有这个HTML代码:
<div id="calc_pol_list">
<li class="calc_li">
<span class="right_list_number">1</span>
<p id="120" class="calc_p"/>
</li>
<li class="calc_li">
<span class="right_list_number">2</span>
<p id="100" class="calc_p"/>
</li>
</div>
I need to get ID of first p: with jQuery and convert this ID to string. 我需要获得第一个p的ID:使用jQuery并将此ID转换为字符串。
I've tried this code: 我试过这段代码:
$('#calc_pol_list p:first').attr('id');
But it doesn't works. 但它不起作用。
How can I do this? 我怎样才能做到这一点?
UPDATE: Here is my function: 更新:这是我的功能:
function refreshCost(){
if ( jQuery.isReady ) {
stomCost = parseInt($('div#calc_pol_list calc_p:first').attr('id'));
var cost = stomCost + scatCost + polCost;
$("#pre_cost").text(cost);
};
};
Yes, my code is correct, if considered separately from the rest of the code. 是的,我的代码是正确的,如果与其余代码分开考虑。 I realized what the problem is, but I do not know how to solve it.
我意识到问题所在,但我不知道如何解决它。 The problem is that these
问题是这些
I add after the Ajax request that is not in their original DOM. 我在Ajax请求之后添加了不在其原始DOM中的内容。 The form, which owns these
形式,拥有这些
is inside the div. 在div里面。 I read here that I need to make the form a direct child of the body, but in this case it's impossible.
我在这里读到,我需要让形式成为身体的直接孩子,但在这种情况下,这是不可能的。
What about this: 那这个呢:
$('#calc_pol_list p').eq(0).attr('id');
jsfiddle http://jsfiddle.net/krasimir/ppzqx/1/ jsfiddle http://jsfiddle.net/krasimir/ppzqx/1/
$('#calc_pol_list p').first().attr('id')
像这样 :)
Solution is to use .ajaxStop 解决方案是使用.ajaxStop
$(document).ajaxStop(function() {
alert($('#calc_pol_list p').eq(0).attr('id'));
});
Try this: 尝试这个:
$(document).ready(function(){
alert($('#calc_pol_list p:first').attr('id'));
});
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
ID和NAME令牌必须以字母([A-Za-z])开头,后面可以跟任意数量的字母,数字([0-9]),连字符(“ - ”),下划线(“_”) ,冒号(“:”)和句号(“。”)。
See: What are valid values for the id attribute in HTML? 请参阅: HTML中id属性的有效值是什么?
Create a variable and get variable in it eg 创建一个变量并在其中获取变量,例如
var content = $('#calc_pol_list > li p:first').attr('id');
and alert it you will get id as string eg 并提醒它你将获得id作为字符串,例如
alert (content );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.