javascript - 用变量预填充 HTML 值?

标签 javascript jquery html css

如何使 HTML 输入字段值预填充变量?在这种情况下,模态主体变量。 我阅读了有关使用 document.write 的信息,但我们如何将其与变量一起使用?

我们正在使用 Mandrill 和完整日历。
非常非常感谢!

代码:

 {
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'))
  var 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();
    }
});
});
});//]]>

          </script>
        <script type="text/javascript"> 

          $(document).ready(function() {
// Generate a simple captcha
function randomNumber(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}

function generateCaptcha() {
    $('#captchaOperation').html([randomNumber(1, 100), '+', randomNumber(1, 200), '='].join(' '));
}

generateCaptcha();

$('#contactForm')
.formValidation({

})
.on('success.form.fv', function(e) {
    // Prevent default form submission
    e.preventDefault();

    // Change these values to match with your application
    var MANDRILL_API_KEY = 'YOUR-MANDRILL-API-KEY',
        EMAIL_SUBJECT    = 'Find Volunteerships - Sign Up Confirmation ',


    var $form = $(e.target),
        // Generate a message based on submitted form data
        body  = [
            '<strong>Name:</strong> ' +     $form.find('[name="firstName"]').val() + ' ' + $form.find('[name="lastName"]').val(),
            '<strong>School</strong> ' + ($form.find('[name="school"]').val() || 'n/a'),
            '',
            '<strong>Message:</strong> ',
            $form.find('[name="school"]').val()
        ].join('<br/>');

    // Send the message
    $.ajax({
        type: 'POST',
        url: 'http://mandrillapp.com/api/1.0/messages/send.json',
        contentType: 'text/plain',
        dataType: 'json',
        data: {
            key: MANDRILL_API_KEY,
            message: {
                from_name: $form.find('[name="fullName"]').val(),
                from_email: $form.find('[name="email"]').val(),
                to: [
                    {
                        email: $form.find('[name="email"]').val(),
                       name: $form.find('[name="fullName"]').val(),
                        'type': 'to'
                    }
                ],
                auto_text: true,
                subject: EMAIL_SUBJECT,
                html: body
            }
        }
    }).done(function(response) {
        // Clear the form
        $form.formValidation('resetForm', true);

// Regenerate the captcha

.on('err.form.fv', function(e) {
        // Regenerate the captcha
        generateCaptcha();
    });

        // Show the message
        response.status === 'error'
            ? $('#alertContainer')
                .removeClass('alert-success')
                .addClass('alert-warning')
                .html('Sorry, cannot register the sign up')
                .show()
            : $('#alertContainer')
                .removeClass('alert-warning')
                .addClass('alert-success')
                .html('Your sign up has been successfully registered')
                .show();
    }).fail(function(jqXHR, textStatus, errorThrown) {
        $('#alertContainer')
            .removeClass('alert-success')
            .addClass('alert-warning')
            .html('Sorry, cannot register the sign up')
            .show();
    });

最佳答案

不确定你的意思,可能是这样的?

modal.find('.' + modal-body)

但是变量不能有带“-”符号的名字..

modal-body”等于“modal - body”,所以它会减去一个。

关于javascript - 用变量预填充 HTML 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35905833/

相关文章:

javascript - 拖动 div 时的 onMouseOver 事件

javascript - 调整 iframe 的宽度和高度以适应其中的内容

jquery - 使用 jQuery 突出显示 textarea 中的 html 标签。可能的?

javascript - 更改智能表中的页面会阻止 JQuery 函数工作?

javascript - 谷歌浏览器中的 getUserMedia

javascript - jquery select2 - 选择带有输入文本字符串的所有选项

javascript - 使用 CSS 内联 JavaScript

javascript - 单击按钮时不调用 Jquery

javascript - 在ajaxed html中调用javascript页面函数

javascript - 可拖动的 div 不可编辑