将全局变量分配给以下Javascript

时间:2016-03-10 14:45:27

标签: javascript jquery html5 fullcalendar mandrill

我们怎样才能使var modal = $(“#modal”);全局变量?

我们在下面的代码中有模态变量,我们需要它才能工作,它目前没有, var content =“Hello”+ name +“,你签署了”+ modal +“直到XYZ”;

很多,非常感谢!

$(document).ready(function() {
$('#calendar').fullCalendar({
  header: {
    left: '',
    center: 'title',
    right: 'prev,next today'
  },
  defaultDate: '2016-03-15',
  events: [

    {
      title: 'Event',
      start: '2016-03-26T11:00:00',
      end: '2016-03-26T12:00:00',
    },
  ],
  eventClick: function(event) {
    console.log(event)
      // alert(event.start.format('MMMM Do YYYY'))
    start = event.start.format('MMMM Do YYYY'),
      end = event.end.format('MMMM Do YYYY'),
      html = '<p>Starts: ' + start + '<p>';
    html += '<p>Ends: ' + end + '<p>';
    var modal = $("#modal");
    modal.find(".modal-title").html(event.title);
    modal.find('.modal-body').html(html)
    modal.modal();
  }
})



 $("#contact_form").submit(function() {
var email = $("#email").val(); // get email field value
var name = $("#name").val(); // get name field value
var msg = $("#msg").val(); // get message field value
var content = "Hello " + name + ", You have signed " + modal + " up to XYZ";
$.ajax({
    type: "POST",
    url: "https://mandrillapp.com/api/1.0/messages/send.json",
    data: {
      'key': 'api',
      'message': {
        'from_email': "email",
        'text': "Hello ",
        'from_name': "name",
        'headers': {
          'Reply-To': "email"
        },
        'subject': 'Confirmation - Sign Up',
        'text': content,
        'to': [{
          'email': email,
          'name': name,
          'type': 'to'
        }]

      }
    }

  })
  .done(function(response) {
    alert('You have been signed up. Thank you!'); // show success message
    $("#name").val(''); // reset field after successful submission
    $("#email").val(''); // reset field after successful submission
    $("#msg").val(''); // reset field after successful submission

  })
  .fail(function(response) {
    alert('Error sending message.');
  });
return false; // prevent page refresh
 });
});

1 个答案:

答案 0 :(得分:0)

使价值全球化的简单解决方案是:

window.modal = "Value";

如果你真的不需要它,但只是想以某种方式存储它,多个函数可以访问它,你可以在函数范围内创建它,如:

(function(){
    var modal = "value";

    $.ajax({
        success: function() {
             console.log(modal);
        }
    })
})()