需要复选框

时间:2019-05-31 20:07:56

标签: javascript ajax checkbox required

我在我的网站(特定的CMS)中具有以下新闻通讯块的代码:

<!-- newsletter block -->

{if $tpl_settings.type == 'responsive_42'}{strip}
<div class="subscribe{if $block.Side != 'left' && $block.Side != 'right'} light-inputs{/if}">
  <div id="nl_subscribe">
    <input placeholder="{$lang.massmailer_newsletter_your_name}" type="text" id="newsletter_name" maxlength="50" />
    <input placeholder="{$lang.massmailer_newsletter_your_e_mail}" type="text" id="newsletter_email" maxlength="100" />

    <br />
    <label><input type="checkbox" id="newsletter_privacy" /></label> <a href="#">Privacy Policy</a>.
    <br />
    <br />

    <input class="low" onclick="xajax_subscribe('subscribe', $('#newsletter_name').val(), $('#newsletter_email').val());$(this).val('{$lang.loading}');" type="button" value="{$lang.massmailer_newsletter_subscribe}" />

    <div class="nav-link"><span id="unsubscribe_link" class="link">{$lang.massmailer_newsletter_unsubscribe}</span></div>
  </div>
  <div id="nl_unsubscribe" class="hide">
    <input placeholder="{$lang.massmailer_newsletter_your_e_mail}" type="text" id="un_newsletter_email" maxlength="50" />
    <input class="low" onclick="xajax_subscribe('unsubscribe', '', $('#un_newsletter_email').val());$(this).val('{$lang.loading}');" type="button" value="{$lang.massmailer_newsletter_unsubscribe}" />
    <div class="nav-link"><span id="subscribe_link" class="link">{$lang.massmailer_newsletter_subscribe}</span></div>
  </div>
</div>
{/strip}{else}
<div id="nl_subscribe">
  {$lang.massmailer_newsletter_your_name}
  <div style="padding: 0 0 5px;"><input type="text" id="newsletter_name" maxlength="150" style="width: 80%;" /></div>

  {$lang.massmailer_newsletter_your_e_mail}
  <div><input type="text" id="newsletter_email" maxlength="100" style="width: 80%" /></div>

  <div style="padding: 10px 0 0;">
    <input onclick="xajax_subscribe('subscribe', $('#newsletter_name').val(), $('#newsletter_email').val());$(this).val('{$lang.loading}');" type="button" value="{$lang.massmailer_newsletter_subscribe}" />
  </div>
  <div style="padding: 5px 0">
    <a id="unsubscribe_link" href="javascript:void(0);" class="static">{$lang.massmailer_newsletter_unsubscribe}</a>
  </div>
</div>
<div id="nl_unsubscribe" class="hide">
  {$lang.massmailer_newsletter_your_e_mail}
  <div><input type="text" id="un_newsletter_email" maxlength="150" style="width: 80%" /></div>
  <div style="padding: 10px 0 0;">
    <input onclick="xajax_subscribe('unsubscribe', '', $('#un_newsletter_email').val());$(this).val('{$lang.loading}');" type="button" value="{$lang.massmailer_newsletter_unsubscribe}" />
  </div>
  <div style="padding: 5px 0">
    <a id="subscribe_link" href="javascript:void(0);" class="static">{$lang.massmailer_newsletter_subscribe}</a>
  </div>
</div>
{/if}

<script type="text/javascript">
  {
    literal
  }
  $(document).ready(function() {
    $('#unsubscribe_link').click(function() {
      $('#nl_subscribe').slideUp('normal');
      $('#nl_unsubscribe').slideDown('slow');
    });
    $('#subscribe_link').click(function() {
      $('#nl_unsubscribe').slideUp('normal');
      $('#nl_subscribe').slideDown('slow');
    });
  }); {
    /literal}
</script>

<!-- newsletter block end -->

我无法通过JavaScript / Ajax强制执行“必需”复选框,请您帮我正确更改代码?

我无法使其正常运行,因为缺少“ form”标签,就像任何普通表单一样。

谢谢!

1 个答案:

答案 0 :(得分:0)

您只需要在启动Ajax请求之前检查输入的值:

function xajax_subscribe(subscribe,name,email) {

    if(email = '') {
        alert('Please enter an E-Mail Address')
    } else {
        // your ajax request
    }

}

请注意,当请求开始时,您必须再次检查分别输入到php文件中的输入值,了解如何检查验证电子邮件等...