获取jQuery中的项目总数

时间:2015-06-03 12:38:45

标签: javascript jquery html

这是我的HTML:

CatManager

我想获得标签的总数,其格式为: <label id='custom'></label> <p>test</p> <label id='start_date[0]'></label> <p>test</p> <p>test</p> <label id='start_date[1]'></label> <label id='start_date[2]'></label> <p>test</p> <label id='custom2'></label> 。我怎样才能做到这一点?你能说点什么吗?感谢。

3 个答案:

答案 0 :(得分:2)

您可以使用'属性开头'选择器和var itemCount = $('label[id^="start_date"]').length; alert(itemCount);属性:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label id='custom'></label>
<p>test</p>

<label id='start_date[0]'></label>
<p>test</p>
<p>test</p>

<label id='start_date[1]'></label>

<label id='start_date[2]'></label>
<p>test</p>

<label id='custom2'></label>
<div>
Hello world
</div>
@section footerButton{
    <input type="button" id="button1" value="Button1"/>
}

答案 1 :(得分:1)

使用{/ 3}}选择器,如

start_date

如果您希望明确以^开头,请使用attribute contains *代替$('[id^="start_date"]').length

length

答案 2 :(得分:1)

您可以使用attribute starts with,然后获取 $('label[id^="start_date"]').length 属性

  

选择具有指定属性的元素,其值始于给定字符串。

$sql="insert into landing_client_data(name,email,contact_no,company,ip,city,region,country,postal,utm_source,utm_medium,utm_term,utm_campaign,created_date) values('".$_POST['name']."', '".$_POST['email']."', '".$_POST['phone']."','".$_POST['company']."','".$ip."', '".$city."', '".$region."', '".$country."', '".$postal."', '".$utm_source."', '".$utm_medium."', '".$utm_term."', '".$utm_campaign."', '".$created_date."')";
		
$res=mysql_query($sql);



//**AFTER THIS I DON'T KNOW WHETHER ITS GOING INSIDE 'if($res)' OR NOT**




if($res){
require('PHPMailer-master/PHPMailerAutoload.php');
$mail = new PHPMailer;
$mail->isSMTP();                                      
$mail->Host = 'smtp.gmail.com';                       
$mail->SMTPAuth = true;                               
$mail->Username = 'test.test6@gmail.com';            
$mail->Password = 'test@123';                        
$mail->SMTPSecure = 'tls';                            
$mail->Port = 587;



$mail->From = 'sachin@aarnasystems.com';
$mail->FromName = 'Sachin Patil CEO, Aarna Systems';                                      
$mail->setFrom('sachin@test.com', 'test');
$mail->addReplyTtesthin@test.com', 'test'); 



$mail->WordWrap = 50;                             
$mail->isHTML(true);                              

$mail->Subject = 'Enquiry: Ecommerce Website Design-';
$mail->Body    = 'Enquiry Details:<br/>
Name: '.$_POST['name'].'<br/>
Email: '.$_POST['email'].'<br/>
Phone: '.$_POST['phone'].'<br/>
Company: '.$_POST['company'].'<br/>';
$mail->send();

// **NoT REACHING HERE**
   
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = '/portfolio_ln/thank-you.php';
header("Location: http://$host$extra");
exit;

}