2016-09-26 25 views
-3

這裏是我的html:如何選擇在jquery中動態更改的id?

<?php echo ($row['status'] == 1)? ?> 
 
    <span id='slide_".$row['id']."' name="slid_id" class='label label-danger'>Inactive</span> 
 
    <?php : ?> 
 
    <span id='slide_".$row['id']."' name="slid_id" class='label label-success'>Active</span> 
 
    <?php ; ?>

這裏是我的jQuery代碼:

$(document).ready(function() 
 
{ 
 
    $("span").click(function() 
 
    { 
 
     \t \t 
 
    var id = $(":input[name=slide_id]").val(); 
 
\t alert(id); 
 
    }); 
 
});

我沒有得到任何東西,我想

ID &其動態生成的值

跨度元件的

,然後執行等上點擊我想改變從活動狀態設置爲無效,反之亦然一些行動。

在此先感謝。

+0

看起來你錯過了兩個功能上的閉合零件。 – mezmi

+0

檢查HTML現在隊友:)謝謝 – Hardy

+0

我無法理解你,這意味着我高於你。 –

回答

0

var id = $(":input[name=slide_id]").attr('id');應該做的工作。 .val()只返回輸入元素的當前值。

+0

不工作它返回undefined :(當試圖獲得選定的編號的價值 – Hardy

-1

只要這樣做:$(selector)[0].id。最簡單的方式,非常容易理解。

var id = $("input")[0].id; 
 
alert(id); 
 
$("input").on("click",function(){alert($("#"+id)[0].value);});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input id="hello" />

+0

我想單擊的跨度元素的ID和它的價值也抱歉,我沒有得到它的代碼感謝任何方式:) – Hardy

0

你的HTML從丟失的問題,但消除結腸中的jQuery選擇可能做的伎倆在選擇輸入字段 - 即$("input[name=slide_id]").val()

如果你想要得到的跨度元素的ID屬性,這會工作:

$('span_selector').attr('id'); 
+0

嘿,我的代碼是最短的 –

+0

html添加隊友請檢查並現在回覆:0 – Hardy

+1

好吧我將HTML更改爲此 name =「slide_id」> 現在在jQuery中使用它來獲取id作爲屬性 $(this).attr(id); 其中提供點擊跨度元素ID; – Hardy

0

還好最後我得到了解決這個事情的方式。如果任何人有一天有相同的問題,那麼這可能會有幫助。

的html代碼:

<tr class="tr"> 
    <td class="td"> 
     <div id="status"> 
      <?php 

      echo ($row['status'] == 1)? 
      "<span id='".$row['id']."' class='label label-danger'>Inactive</span>": 
      "<span id='".$row['id']."' class='label label-success'>Active</span>"; 
      ?> 
     </div> 
    </td> 
</tr> 

我jQuery代碼:

$(document).ready(function() 
    { 
     $("#status > span") .click(function() 
     { 

      var id = $(this).attr('id'); 
      var tempelement = $(this); 
      var texts = $(this).closest(".tr").find("#texts").val(); 
      var author = $(this).closest(".tr").find("#author").val(); 
      var status = $(this).text(); 
      $.ajax({ 
        type: 'POST', 
        url: 'manage_feedback.php', 
        data: {id:id, texts:texts, author:author, status:status}, 
        success: function(data) 
        { 
         if (data == "Active") 
         { 
          $(tempelement).removeClass("label label-danger"); 
          $(tempelement).addClass("label label-success"); 
          $(tempelement).html(data); 
          alert('status changed'); 
         } 
         else if (data == "Inactive") 
         { 
          $(tempelement).removeClass("label label-success"); 
          $(tempelement).addClass("label label-danger"); 
          $(tempelement).html(data); 
          alert('status changed'); 
         } 
         else 
         { 
          alert(data); 
         } 
        } 
      }); 
     }); 

php腳本

//ajax status changer code 
 
if (isset($_POST['id']) && isset($_POST['texts']) && isset($_POST['status']) && isset($_POST['author'])) 
 
{ 
 
\t $id = $_POST['id']; 
 
\t $texts = trim($_POST['texts']); 
 
\t $author = trim($_POST['author']); 
 
\t $status = $_POST['status']; 
 

 
\t $qry = "SELECT count(id) as count FROM tbl_testimonials WHERE texts = '".$texts."' 
 
\t \t \t AND author = '".$author."' AND id != ".$id." "; 
 
\t $sql = mysql_query($qry); 
 
\t $data = mysql_fetch_assoc($sql); 
 
\t 
 
\t if ($data['count'] == 0) 
 
\t { 
 
\t \t if($status == 'Inactive') 
 
\t \t { 
 
\t \t \t $qry = "UPDATE tbl_testimonials SET status = 0 WHERE id = ".$id." " ; 
 
\t \t \t $sql = mysql_query($qry); 
 
\t \t \t if($sql == 1) 
 
\t \t \t { 
 
\t \t \t \t echo 'Active'; 
 
\t \t \t \t exit; 
 
\t \t \t } 
 
\t \t } 
 
\t \t elseif ($status == 'Active') 
 
\t \t { 
 
\t \t \t $qry = "UPDATE tbl_testimonials SET status = 1 WHERE id = ".$id." " ; 
 
\t \t \t $sql = mysql_query($qry); 
 
\t \t \t if($sql == 1) 
 
\t \t \t { 
 
\t \t \t \t echo 'Inactive'; 
 
\t \t \t \t exit; 
 
\t \t \t } 
 
\t \t } 
 
\t } 
 
\t else 
 
\t { 
 
\t \t echo "name already taken"; 
 
\t \t exit; 
 
\t } 
 
}

希望它能幫助別人。