我正在尝试创建一个小表单,提交data-id
li
并传递一个值li
。这是我到目前为止所做的......
<form method="POST" action="/default/index" id="event-notify-form">
<div id="notification-event" style="overflow: hidden; width: auto; height: 400px">
<ul>
<li data-id="171" class="nav-message-body"></li>
<li data-id="168" class="nav-message-body"></li>
</ul>
</div>
</form>
我的jquery是
$('#notification-event li').click(function() {
var eid=$(this).attr("data-id");
$('#event-notify-form').append(eid).submit();
});
data-id不发布,我试过$_POST
它显示空数组
答案 0 :(得分:6)
创建隐藏的输入元素并将其值设置为
<form method="POST" action="/default/index" id="event-notify-form">
<input type="hidden" name="myId" />
<div id="notification-event" style="overflow: hidden; width: auto; height: 400px">
<li data-id="171" class="nav-message-body"></li>
<li data-id="168" class="nav-message-body"></li>
</div>
</form>
然后
$('#notification-event li').click(function () {
var eid = $(this).attr("data-id");
var $frm = $('#event-notify-form');
//set the value of the hidden element
$frm.find('input[name="myId"]').val(eid);
//submit the form
$frm.submit();
});
在服务器端
$_POST('myId')
答案 1 :(得分:0)
尝试改为:
$('#notification-event li').click(function() {
var eid=$(this).attr("data-id");
$('#event-notify-form').append("<input />", {
"type" : "hidden",
"value" : eid,
"name" : "some_name"
});
$('#event-notify-form').submit();
});