我有一个动态的记录列表,我在一个表中显示,每行有两个按钮,一个用户到一个页面查看/编辑数据,另一个用于删除行项。< / p>
我想弄清楚的是,当选择其中一个按钮时,如何创建javascript以仅为该行提取lead_id隐藏值。
我理解如何使用js从命名元素的单个结果中提取值,只是在理解如何从单击按钮的行中检索单个结果时遇到问题。
这是HTML:
<table id="lead_list" width="100%">
<th>
<tr>
<td><strong>Date</strong></td>
<td><strong>First Name</strong></td>
<td><strong>Last Name</strong></td>
<td><strong>Email</strong></td>
<td><strong>Company</strong></td>
<td><strong>Lead Status</strong></td>
<td><strong>Actions</strong></td>
</tr>
</th>
<tr>
<td>2014-03-06</td>
<td>Bob</td>
<td>Wilson</td>
<td><a href="mailto:test@test.com">test@test.com</a></td>
<td>MyFunLIFE</td>
<td>Subscribed</td>
<td><input type="hidden" name="lead_id" value="379" />
<input type="button" class="view-edit-lead" value="View/Edit" />
<input type="button" class="delete-lead" value="Delete" /></td>
</tr>
<tr>
<td>2014-03-06</td>
<td>Tom</td>
<td>Shuemate</td>
<td><a href="mailto:test@test.com">test@test.com</a></td>
<td>MyFunLIFE</td>
<td>Subscribed</td>
<td><input type="hidden" name="lead_id" value="377" />
<input type="button" class="view-edit-lead" value="View/Edit" />
<input type="button" class="delete-lead" value="Delete" /></td>
</tr>
<tr>
<td>2014-03-06</td>
<td>Biggie</td>
<td>Smalls</td>
<td><a href="mailto:test@test.com">test@test.com</a></td>
<td>MyFunLIFE</td>
<td>Subscribed</td>
<td><input type="hidden" name="lead_id" value="376" />
<input type="button" class="view-edit-lead" value="View/Edit" />
<input type="button" class="delete-lead" value="Delete" /></td>
</tr>
</table>
这是我正在使用的当前js:
$('td.wplp-inputs input').click(function(){
// Don't allow access to the page while we process data.
$.blockUI({
message: '<h1>Just a moment</h1><br /><br />Lead is being deleted...',
css: {
top: ($(window).height() - 400) /2 + 'px',
left: ($(window).width() - 400) /2 + 'px',
width: '400px',
Height: '250px',
padding: '10px'
}
});
// if the button class is
if( $(this).is('.delete-lead') ) {
/// delete
data = new Object();
data.lead_id = $(this).siblings('input[type="hidden"]').val();
alert(JSON.stringify(data));
data.action = "delete_lead"; //the action to call
data._ajax_nonce = wplpajaxobjfront.nonce; // This is the name of the nonce setup in the localize_script
data.ajaxUsed = "yes";
// Define the URL for the AJAX to call
var url = wplpajaxobjfront.ajaxurl;
//alert( JSON.stringify( data ) );
//alert( JSON.stringify( url ) );
$.post(url, data, function(response) {
alert(response);
window.location.reload();
//window.location.href = response;
$.unblockUI();
//window.open(response,"_blank","","");
});
} else {
/// edit/view
alert(reponse);
}
return false; //Do not remove!
});
一旦我有了从单个表行获取数据的方法,特别是lead_id,我将能够毫无问题地将它传递给我的PHP函数。
js的问题还在于不允许将其他值传递给var数据,当前从ajax调用返回的唯一项是没有包含输入名的lead_id,对于data.action,data.ajaxUsed和data都是一样的._ajax_nonce等。
提前感谢您对此有任何帮助。
答案 0 :(得分:0)
这是在按钮单击
时每行获取lead_id
值的简单方法
$('td input').click(function(){
var el = $(this).siblings('input[type="hidden"]');
// to get input name
var data = {}; /// create new object
data[el.attr('name')] = el.val() ; // lead_id to data
data['action'] = 'delete_lead'; // add action to data
console.log(data); // log data to check, open your console and see data
if( $(this).is('.delete-lead') ) {
/// delete
} else {
/// edit/view
}
});