[英]Pass a global variable to the $(document).ready function in jQuery
[英]Passing in a global javascript array to jQuery document(ready)
我有以下从模板渲染的html(jsRender)
<div class="noteActions top" style="z-index: 3;">
<span onclick="noteAction('add', 13808, 0 );"></span>
<span onclick="noteAction('update',13808, 106344 );"></span>
<span onclick="noteAction('delete', 13808, 106344 );"></span>
</div>
我的问题是我在文档外部准备了一个函数,该函数设置了一个数据数组,稍后,一个jQuery对话框窗口通过ajax提交给处理程序以更新数据库
发生的情况是,数据数组通过类选择器(pr-body,pr-title)正确传递了除jquery vals之外的所有内容,它们作为NULL传递
javascript-外部文档(就绪)
var updateUrl = 'handlers/Poster.ashx',
data;
function noteAction(action, prospectID, noteID){
data = {
'operation': action,
'prospectid':prospectID,
'note-body' : $('.pr-body').val(),
'note-title' : $('.pr-title').val(),
'note-id':noteID,
};
if (action == 'add'){
$( "#dialogPostIt" ).dialog("open", "option", "title", "Add Post It");
} else if (action == 'update'){
$( "#dialogPostIt" ).dialog("open", "option", "title", "Edit Post It");
} else if (action == 'delete'){
if (!confirm('Are you sure you want to delete')) return false;
$.post(updateUrl+"?operation=delete¬eid="+noteID, function(data) {
$('#stickyNote-'+noteID).remove();
});
}
}
jQuery-文档准备就绪
$(document).ready(function() {
$( "#dialogPostIt" ).dialog({autoOpen: false, modal:true,
buttons: {
'Save': function() {
$.ajax({
url: updateUrl,
data: data,
success: function(json, textStatus, jqXHR){
.....
html
<div id="dialogPostIt" >
<form id="postItNow" action="" method="post" class="note-form">
<label for="note-title">Title (description)</label>
<input type="text" name="note-title" id="note-title" class="pr-title" value="" />
<label for="note-body">Text of the note</label>
<textarea name="note-body" id="note-body" class="pr-body" cols="30" rows="6"> </textarea>
</form></div>
我以前是在对话框保存按钮function()内设置数据数组,该方法工作正常,但我需要根据事件使某些数组元素动态化
该数组不必从我的要求是全局的,我只是想不出另一种方法来做到这一点
一如既往,我们将不胜感激任何帮助
好吧,我感觉像是真正的涂料,实际上工作正常,问题是飞行员错误-_-
数据数组正确返回值,问题是还没有值 ,因为数据是在包含表单的下一个对话框之前设置的,因此尚无法填写表单值
修复
准备好外部javascript文件
function noteAction(action, prospectID, noteID){
data = {
'operation': action,
'prospectid':prospectID,
'notebody' : '',
'notetitle' : '',
'noteid':noteID,
};
对话框中的jQuery(准备好文档)
$( "#dialogPostIt" ).dialog({autoOpen: false, modal:true,
buttons: {
'Save': function() {
data.notebody = $('.pr-body').val();
data.notetitle= $('.pr-title').val(),
$.ajax({
url: updateUrl,
data: data,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.