【问题标题】:Passing in a global javascript array to jQuery document(ready)将全局 javascript 数组传递给 jQuery 文档(准备就绪)
【发布时间】:2013-02-07 17:20:01
【问题描述】:

我有以下从模板 (jsRender) 渲染的 html

 <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&noteid="+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()中设置数据数组,效果很好,但是我需要根据事件使一些数组元素动态

根据我的要求,数组不必是全局的,我只是想不出另一种方法来做到这一点

一如既往,非常感谢任何帮助

【问题讨论】:

  • 为什么你在 jQuery 中使用内联 JS 调用?
  • 显然不是最优的,第三方限制,打算探索变成on('click')s,但我不认为这是上面代码的问题,试图弄清楚为什么只有两个选择器没有被传递

标签: javascript jquery ajax jquery-ui


【解决方案1】:

好吧,我觉得自己像个傻瓜,它实际上工作正常,问题是飞行员错误-_-

数据数组正确返回值,问题是 还没有值,因为数据是在包含表单的后续对话框之前设置的,所以还没有填写表单值

修复

文档外的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,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 2012-10-16
    • 2017-07-05
    相关资源
    最近更新 更多