将点击链接的值存储到隐藏的帖子中 [英] store values from clicked link into hidden input for post
问题描述
当用户点击时有可能会出现这样的情况:
我有一个表格中每个条目的更多信息链接(如屏幕截图所示)更多的信息链接,它抓取该链接的价值,将其存储在一个隐藏的输入字段,所以在后我可以抓住这些值?
更多信息链接的命名约定是:
< a href =#data-student =2class =mini-view>详情< / A>
< a href =#data-student =6class =mini-view> more< / a>
< a href =#data-student =7class =mini-view> more< / a>
< a href =#data-student =9class =mini-view> more< / a>
所以如果所有链接都被点击了,它会发布如下数据:2,6,7, 9 - 用户可以多次点击更多信息链接,因此我只想在第一次点击时记录它。
因为我我会将这些值转换为一个数组,并使用它来做一些后端检查。
var clickedIds =''; $('。mini-view')。on('click',function(){
//需要检查more info是否已经被点击
if(!$(this).data('clicked')){
//如果没有,更新点击学生ID的序列化列表:
clickedIds = clickedIds + $(this).data('student')+',';
//将该值更新到隐藏字段
//可以做到这一点w / o clickedIds var,但我认为它更清洁
//这样:
$('#myHiddenField')。val(clickedIds);
//然后将其标记为已被点击。
$(this).data('clicked',true)
}
});
这将一个序列化的列表放入您的隐藏变量,看起来像'2,6,7,9, '
I have a more info link for each entry in a table (as seen in screenshot), that displays additional information.
Is it possible for when a user clicks the more info link, it grabs the value from that link, store it in a hidden input field, so on post I can grab those values?
the naming convention of the "more" info link is:
<a href="#" data-student="2" class="mini-view">more</a>
<a href="#" data-student="6" class="mini-view">more</a>
<a href="#" data-student="7" class="mini-view">more</a>
<a href="#" data-student="9" class="mini-view">more</a>
So if all links were clicked, it'd post the data like: 2,6,7,9 - the user can click the "more" info link as many times as they want, so I'd only want to record it on the first click.
Because I am going to convert those values into an array and use that to do some backend checks.
var clickedIds = '';
$('.mini-view').on('click', function(){
// need to check if the "more info" has already been clicked
if(!$(this).data('clicked')){
//if not, update the (serialized) list of clicked student ids:
clickedIds = clickedIds + $(this).data('student') + ',';
//update that value to the hidden field
//could have done this w/o the clickedIds var, but I think it's cleaner
//this way:
$('#myHiddenField').val(clickedIds);
// then mark it as already clicked.
$(this).data('clicked', true)
}
});
This puts a serialized list into your hidden variable that looks like '2,6,7,9,'
这篇关于将点击链接的值存储到隐藏的帖子中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!