javascript - 先前 Jquery 事件设置的未定义值
问题描述
我正在尝试通过获取未定义的响应来在单击事件上发布具有动态设置值的表单这是我的代码。
$( ".paymentselect" ).click(function() {
var bookingid=$(this).data('id');
$('#paymentstats').css('display','block');
$('#paymentform').attr('action', document.location.origin +"/accounts/" + bookingid);
});
$('#paymentform').submit(function(event){
event.preventDefault();
var data=$('#paymentform').serialize();
$.ajax({
url: $(this).attr('action'),
method: 'POST',
data: data,
beforeSend: function() {
// setting a timeout
$('#wait').html('<i class="material-icons">cached</i>');
},
success: function(data){
if(data.status){
$('#paymentstats').css('display','none');
alert("UPDATED");
//location.reload();
}else
{
alert("something went wrong");
}
}
});
});
在 POST bookingid上是未定义的。小帮助将被appriciated
编辑:
<table>
<tr>
<td>
<button class="paymentselect" data-id="NH7003687415654"> Update</button>
</td>
</tr>
<table>
<div id="paymentstats" class="custom-modal">
<!-- Modal content-->
<div class="modal-content">
<div class="card" style="margin-bottom:0px">
<form id="paymentform" method="POST"> //form that will be posted
<div class="body">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<div class="form-line">
<input type="text" class="form-control" id="utr" name="utr" placeholder="" required>
<input type="hidden" class="form-control" id="bookingpaymentid" name="id" placeholder="">
</div>
<label class="small-label" for="utr">Payment UTR(if any)</label>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<div class="form-line">
<input type="text" class="form-control" id="ref" name="ref" placeholder="" required>
</div>
<label class="small-label" for="ref">Payment Ref</label>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<div class="spin" id="wait"></div>
<button type="submit" class="btn btn-raised bg-pink waves-effect">Submit</button>
<button type="button" class="btn btn-raised bg-red waves-effect" id="close-btn">Close</button>
</div>
</form>
</div>
</div>
根据要求上面是我要求的html代码。我已经尝试在点击事件上设置bookingpaymentid Val但是当表单在服务器上发布时它也显示未定义。
我刚刚发现当我发布表单时该值变得未定义。任何建议
解决方案
因为你bookingid
在里面声明$( ".paymentselect" ).click(function() {
。你可以试试这个:
var bookingid = null;
$( ".paymentselect" ).click(function() {
bookingid=$(this).data('id');
$('#paymentstats').css('display','block');
$('#paymentform').attr('action', document.location.origin +"/accounts/" + bookingid);
});
推荐阅读
- php - 从webview下载pdf,pdf以blob格式存储在mysql DB中
- javascript - 如何在反应可排序树中复制树节点(带子节点)并粘贴到树中的其他位置?
- sql - SQL - 计算课程部分的数量
- swift - 磁盘工具列表中使用了哪些 UI 元素
- javascript - 判断用户的浏览器是否支持 USDZ 或 Reality Composer 文件格式
- python - 如何修复 Python Whle Scraping 中的 If 语句
- python - 列表在通过其他文件中的 removeEven 函数后返回空
- flutter - 使用firestore和flutter从子集合中查询特定文档
- sql - 为应用程序使用的所有表提供唯一 ID 的最佳做法是什么?
- javascript - 如何在 React.js 中打印当前时间?