2016-10-19 59 views
-1

我有一个表单应该根据复选框更改操作href。一个动作是contact.html,另一个动作是链接到负责订阅的外部页面(新邮件)。例如https://stackoverflow.com。如果我将操作更改为https://stackoverflow.com,表单不会将邮件发送到我的电子邮件,但只会发送到https://stackoverflow.com 如何将其更改为始终发送电子邮件,但只有在复选框被选中时才能注册到新鲜邮件?更改操作href

$('#site.contact #left :checkbox').change(function() { 
    if($(this).is(":checked")) { 
    console.log('checked'); 
    $('.jsForm').attr('action', 'https://app.freshmail.com/pl/actions/subscribe/'); 
} else { 
    $('.jsForm').attr('action', 'kontakt.html'); 
    console.log('unchecked'); 
} 

上面的代码改变动作完美,但接下来呢?

+0

注册您的服务器上 – mplungjan

+0

如果我能我不会问 – user3516973

+0

的答案是使用卷曲或类似的服务器上。你没有告诉我们服务器语言。您在服务器上阅读检查的状态,并按照相应的方式进行操作。你不能让一个表单做两个动作,除非你对你的服务器ajax,然后提交到订阅 – mplungjan

回答

0
$('#idcheck').change(function() { 
    if($(this).is(":checked")) { 
    alert("ola"); 
    console.log('checked'); 
    $('.jsForm').prop('action', 'https://app.freshmail.com/pl/actions/subscribe/'); 
} else { 
    $('.jsForm').prop('action', 'kontakt.html'); 
    console.log('unchecked'); 
} 

}); 

尝试使用prop而不是attr和close change(function(){code});

+0

我写道,改变行动的作品。如果勾选复选框,我想执行2个操作。一个是外部链接,另一个是contact.php – user3516973

0

试试这个

$(function() { 
    $('#jsForm').on('submit', function(e) { 
    if ($("#subscribe").is(":checked")) { 
     e.preventDefault(); // stop submission 
     $(this).prop('action', 'https://app.freshmail.com/pl/actions/subscribe/'); 
     $.get("kontakt.html",function(data) { 
     $("#result").html(data); // success 
     setTimeout(function() { 
      $('#jsForm').submit(); // note: there can be nothing name="submit" in the form 
     },2000); // give them a chance to read 
     }); 
    }); 
    } 
}); 

使用

<form id="jsform" action="kontakt.html"> 
    <input type="checkbox id="subscribe" /> 
    <input type="submit" /> 
</form> 
<span id="result"></span>