jQuery事件不会在HTML表单提交时触发

时间:2016-11-23 20:12:50

标签: javascript jquery html forms

如果这个问题已被提出,我很抱歉,但我尝试的所有解决方案都没有对我有用。我想要它做的是在提交表单元素时触发一些jQuery代码。 这是我的代码。

JS:

let coord1 = CLLocation(latitude: 52.12345, longitude: 13.54321)
let coord2 = CLLocation(latitude: 52.45678, longitude: 13.98765)
let coord3 = CLLocation(latitude: 53.45678, longitude: 13.54455)

let coordinates = [coord1, coord2, coord3]

let userLocation = CLLocation(latitude: 52.23678, longitude: 13.55555)

let closest = coordinates.min(by: 
{ $0.distance(from: userLocation) < $1.distance(from: userLocation) })

HTML:

$(document).ready(function() {
    $('form').bind('submit', function() {
        alert('hi');
            var word10 = <?php echo $word10; ?>;
            var word20 = <?php echo $word20; ?>;
            var word1Txt = <?php echo $word1Txt; ?>;
            var word2Txt = <?php echo $word2Txt; ?>;

            $('div.word1').prepend("<h3 class='header'>hi" + word1Txt + "</h3>");
            $('div.word2').prepend("<h3 class='header'>hi" + word2Txt + "</h3>");
    });
});

1 个答案:

答案 0 :(得分:1)

使用.on()或.submit()代替,如果您的vars应该是字符串,请引用它们:

$(document).ready(function() {
    $('form').on('submit', function() {
        alert('hi');
            var word10 = '<?php echo $word10; ?>';
            var word20 = '<?php echo $word20; ?>';
            var word1Txt = '<?php echo $word1Txt; ?>';
            var word2Txt = '<?php echo $word2Txt; ?>';

            $('div.word1').prepend("<h3 class='header'>hi" + word1Txt + "</h3>");
            $('div.word2').prepend("<h3 class='header'>hi" + word2Txt + "</h3>");
    });
});