English 中文(简体)
Jquery div由id选定,选择班级价值
原标题:Jquery div is selected by id, choose the value of class
  • 时间:2011-11-04 19:07:14
  •  标签:
  • php
  • jquery

how do i do that ? my html structure looks like this EDIT: i have updated to html so it fits the rules.

<td id="1" class="LinkWrap"><a href="" class="link">link</a></td>

i 采用该守则:

$(".link").click(function() {
$(this).parent();
//some ajax code
});

To select the td by the id LinkWrap, but i need the value of the class, the class value is fetched by php && mysql and is the id of the element thats i fetched. therefor the value is not a constant i and i cant just say chose class with value 1 it could be 2 or 5932.

i need that value to my ajax script so i can insert when the user clicks that link. if theres a better method please let me know :)

最佳回答

除了不建议以若干人开始分类之外。 使用attr ,以获得该类特性。

$(".link").click(function() {
   alert( $(this).parent().attr( class ) ); 
});

Edit:你更妥善地利用他人所述数据属性。

传真

 <td data-rowid="1" class="LinkWrap"><a href="" class="link">link</a></td> 

Java

$(".link").click(function() {
   alert( $(this).parent().data( rowid ) ); 
});
问题回答

如你在<条码><>/代码>上重新进行数据储存。 班级不能用一位数开始,这意味着你的无效使用可能会导致一些浏览器中的冰川化。 更不用使用j Query s data(>)和data-*属性。

Interstellar_Coder and wilbbe01 are both right in the execution but there is one thing in your code that is going to bite you. The td has an ID that, if repeated, could cause you referencing issues. id attributes should be unique across a DOM instance at peril of very unpredictable results across different browsers.

除了这一奇特加兹解决方案外,最好的是。

由于正如我在评论中提到的那样,你似乎混杂了这些班子和工地,为了避免塞伊杰罗人指出的问题,我建议采用另一种做法:

$( .link ).click(function(e){
   var the_id = $(this).closest( td ).attr( id );  //or parent().
   $.ajax({
       url:  http://www.mysite.com/index.php?page_id= +the_id,
       success:function(response){
         alert(response);
       }
   });
  e.preventDefault();
});

阁下:

<td class="LinkWrap" id="td_1"><a href="" class="link">link</a></td>

And in your php:

$id = $_GET[ page_id ];
$id = str_replace( td_ ,  ,$id);
echo $id;

I think what you will need is the following.

$(this).parent().attr( class );

Edit: Ditto the data属性也由他人说明。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签