2013-11-04 35 views
1

我在使用parent()從兩個輸入框中獲取值時出現問題。和next()函數。 。在我的腦海中,這應該工作!任何人都可以看到我錯過了什麼?在Jquery中使用next和parent獲取隱藏輸入

謝謝。

我有這個HTML

<tr> 
<td class='headline2' style="width: 200px; padding-bottom: 20px; padding-top:20px; padding-left: 10px; text-align: left;"><?= $value['f_name'] . " " . $value['l_name']; </td> 
<td class='headline2'><?= $value[0]->end ?><br><?= $value[0]->start ?></td> 
<td class='headline2'style='padding-right:18px;'><?= $value[0]->updated ?></td> 
<td><div class="approve"><input disabled <? if ($value[0]->studentApproved == 1) { ?>checked<? } ?> type="checkbox"><label><span></span></label></div></td> 
<td><div class="approve"><input disabled <? if ($value[0]->companyApproved == 1) { ?>checked<? } ?> type="checkbox"><label><span></span></label></div></td> 
<td><div class="approve"><input class="approveME" value="<?= $value[0]->application_id ?>" <? if ($value[0]->koordinatorApproved == 1) { ?>checked<? } ?> id="<?= $i ?>" type="checkbox"><label for="<?= $i ?>" ><span></span></label></div></td> 
<td><input class="deleteME "type="image" src="../wp-content/themes/tutorial_theme/images/DELETE.png"><td> 
</tr> 
<input class="hidden" value="<?= $value[1] ?>"> 
<input class="hidden" value="<?= $value[0]->user_id ?>"> 

JQUERY

$('.content').on('click', '.deleteME', function() { 
    var class_id = $(this).parent().parent().next().val(); 
    var user_id = $(this).parent().parent().next().next().val(); 

    $.ajax({ 
     type: "POST", 
     url: (my_ajax_script.ajaxurl), 
     data: ({action: 'delete_student_from_class', class_id: class_id, student_id: user_id}), 
     success: function(msg) { 
      alert("data something" + msg); 
     } 

    }); 
}); 
+1

你可以直接給id給隱藏的輸入,並且可以得到這些值而不是冗長的腳本 – SRy

+0

其中是放置$('。content')元素嗎?我猜你使用$(this)得到錯誤的參考... – sissy

+0

你的'

'標籤在哪裏?你也需要一個'parent()'。 – zgood

回答

1

你不應該在表的HTML元素直接而你需要把他們在某些TD。 html應該被改變。您可以在.deleteME之後的新td中的同一行中添加隱藏字段。

的Html

tr> 
<td class='headline2' style="width: 200px; padding-bottom: 20px; padding-top:20px; padding-left: 10px; text-align: left;"><?= $value['f_name'] . " " . $value['l_name']; </td> 
<td class='headline2'><?= $value[0]->end ?><br><?= $value[0]->start ?></td> 
<td class='headline2'style='padding-right:18px;'><?= $value[0]->updated ?></td> 
<td><div class="approve"><input disabled <? if ($value[0]->studentApproved == 1) { ?>checked<? } ?> type="checkbox"><label><span></span></label></div></td> 
<td><div class="approve"><input disabled <? if ($value[0]->companyApproved == 1) { ?>checked<? } ?> type="checkbox"><label><span></span></label></div></td> 
<td><div class="approve"><input class="approveME" value="<?= $value[0]->application_id ?>" <? if ($value[0]->koordinatorApproved == 1) { ?>checked<? } ?> id="<?= $i ?>" type="checkbox"><label for="<?= $i ?>" ><span></span></label></div></td> 
<td><input class="deleteME "type="image" src="../wp-content/themes/tutorial_theme/images/DELETE.png"><td> 
<td> 
<input class="hidden" value="<?= $value[1] ?>"> 
<input class="hidden" value="<?= $value[0]->user_id ?>"> 
</td> 
</tr> 

的Javascript

var class_id = $(this).closest('tr').find('.hidden:eq(0)').val(); 
var user_id = $(this).closest('tr').find('.hidden:eq(1)').val(); 

或者

var class_id = $(this).parent().next('td').find('.hidden:eq(0)').val(); 
var user_id = $(this).parent().next('td').find('.hidden:eq(1)').val(); 
+0

當使用.closest('tr')apporach時,這對我有用,謝謝! :) – Pjust

+0

不客氣。 – Adil

0

parent()可能會比較混亂,當你的DOM變化,它會破壞你的代碼。

也許你可以嘗試這樣的事:

HTML

<tr> 
<td class='headline2' style="width: 200px; padding-bottom: 20px; padding-top:20px; padding-left: 10px; text-align: left;"><?= $value['f_name'] . " " . $value['l_name']; </td> 
<td class='headline2'><?= $value[0]->end ?><br><?= $value[0]->start ?></td> 
<td class='headline2'style='padding-right:18px;'><?= $value[0]->updated ?></td> 
<td><div class="approve"><input disabled <? if ($value[0]->studentApproved == 1) { ?>checked<? } ?> type="checkbox"><label><span></span></label></div></td> 
<td><div class="approve"><input disabled <? if ($value[0]->companyApproved == 1) { ?>checked<? } ?> type="checkbox"><label><span></span></label></div></td> 
<td><div class="approve"><input class="approveME" value="<?= $value[0]->application_id ?>" <? if ($value[0]->koordinatorApproved == 1) { ?>checked<? } ?> id="<?= $i ?>" type="checkbox"><label for="<?= $i ?>" ><span></span></label></div></td> 
<td><input class="deleteME data-class="<?= $value[1] ?>" data-user="<?= $value[0]->user_id ?>" "type="image" src="../wp-content/themes/tutorial_theme/images/DELETE.png"><td> 
</tr> 

JS

$('.content').on('click', '.deleteME', function() { 
    var class_id = $(this).data('class'); 
    var user_id = $(this).data('user'); 

    $.ajax({ 
     type: "POST", 
     url: (my_ajax_script.ajaxurl), 
     data: ({action: 'delete_student_from_class', class_id: class_id, student_id: user_id}), 
     success: function(msg) { 
      alert("data something" + msg); 
     } 

    }); 
}); 

參考:jQuery .data()

從jQuery 1.4.3開始,HTML 5數據屬性將被自動引入到jQuery的數據對象中。

相關問題