根据其他输入字段

时间:2015-10-06 14:34:37

标签: javascript jquery html css wordpress

很抱歉原帖不清楚。

我试图在未选中底部复选框时隐藏前两个表单字段区域,并且在隐藏任何值时不传递任何值。它是wordpress的注册表单,我不想修改html,这会使它变得更容易,因为我可以接下来div中的标签和输入来隐藏。

此处显示ID:

(顶部复选框) WS-插件 - s2member定制-REG-场通讯 (中间电台) WS-插件 - s2member定制-REG-场jobalerts (底部复选框ID) WS-插件 - s2member定制-REG-场选择在

@Mosh下面的建议很接近,但只隐藏了一个。



// Store the `p` node which contains the label.
var collapseParent = $('label[for="ws-plugin--s2member-custom-reg-field-jobalerts"]').parent();

// Store the `checkbox` that we want to "listen" to
var checkbox = $('[name="ws_plugin__s2member_custom_reg_field_opt_in"]');

// Attach the `change` event on the checkbox and trigger the event so it will fire on page load
checkbox.change(toggle).trigger('change');

// show/hide the `p` node depeneds if the checbox was checked or not
function toggle() {
    collapseParent.toggle(checkbox.is(':checked'));
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
    <label for="ws-plugin--s2member-custom-reg-field-newsletters">
<span style="display:none;">Do you wish to receive JETAAUK Newsletters? *</span>

    </label>
    <input type="checkbox" value="1" checked="checked" name="ws_plugin__s2member_custom_reg_field_newsletters" id="ws-plugin--s2member-custom-reg-field-newsletters" aria-required="true" tabindex="59" class="ws-plugin--s2member-custom-reg-field s2conditional1">
    <label for="ws-plugin--s2member-custom-reg-field-newsletters" style="display:inline !important; margin:0 !important;">Do you wish to receive JETAAUK Newsletters?</label>
</p>
<p>
    <label for="ws-plugin--s2member-custom-reg-field-jobalerts">
<span>Do you wish to receive job alerts (digest)? *</span>

    </label>
    <br>
    <input type="radio" value="weekly" name="ws_plugin__s2member_custom_reg_field_jobalerts" id="ws-plugin--s2member-custom-reg-field-jobalerts---0" aria-required="true" tabindex="60" class="ws-plugin--s2member-custom-reg-field s2conditional2" style="color:#006699;">
    <label for="ws-plugin--s2member-custom-reg-field-jobalerts-0" class="ws-plugin--s2member-custom-reg-field-op-l" style="display:inline !important; margin:0 !important;">weekly</label>&nbsp;&nbsp;
    <input type="radio" value="daily" name="ws_plugin__s2member_custom_reg_field_jobalerts" id="ws-plugin--s2member-custom-reg-field-jobalerts---1" aria-required="true" tabindex="60" class="ws-plugin--s2member-custom-reg-field s2conditional2" style="color:#006699;">
    <label for="ws-plugin--s2member-custom-reg-field-jobalerts-1" class="ws-plugin--s2member-custom-reg-field-op-l" style="display:inline !important; margin:0 !important;">daily</label>&nbsp;&nbsp;
    <input type="radio" value="none" name="ws_plugin__s2member_custom_reg_field_jobalerts" id="ws-plugin--s2member-custom-reg-field-jobalerts---2" aria-required="true" tabindex="60" class="ws-plugin--s2member-custom-reg-field s2conditional2" style="color:#006699;">
    <label for="ws-plugin--s2member-custom-reg-field-jobalerts-2" class="ws-plugin--s2member-custom-reg-field-op-l" style="display:inline !important; margin:0 !important;">none</label>
</p>
<p>
    <label for="ws-plugin--s2member-custom-reg-field-opt-in">
        <input type="checkbox" name="ws_plugin__s2member_custom_reg_field_opt_in" id="ws-plugin--s2member-custom-reg-field-opt-in" class="ws-plugin--s2member-custom-reg-field" value="1" checked="not" tabindex="70">
<span style="color:#be0026;"><strong>=&gt; Yes, I wish to receive JETAAUK Newsletters via email &lt;= </strong></span>

    </label>
</p>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

这段代码有点难看,但这是不改变html的“成本”。

请阅读代码中的注释,以便了解我的所作所为。

如果要对其他复选框/单选按钮执行此功能,只需更改元素的选择器(标签/复选框或p节点)

// Store the `p` node which contains the label.
var collapseParent = $('label[for="ws-plugin--s2member-custom-reg-field-jobalerts"]').parent();

// Store the `checkbox` that we want to "listen" to
var checkbox = $('[name="ws_plugin__s2member_custom_reg_field_newsletters"]');

// Attach the `change` event on the checkbox and trigger the event so it will fire on page load
checkbox.change(toggle).trigger('change');

// show/hide the `p` node depeneds if the checbox was checked or not
function toggle() {
  collapseParent.toggle(checkbox.is(':checked'));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
<label for="ws-plugin--s2member-custom-reg-field-newsletters">
<span style="display:none;">Do you wish to receive JETAAUK Newsletters? *</span></label>
<input type="checkbox" value="1" checked="checked" name="ws_plugin__s2member_custom_reg_field_newsletters" id="ws-plugin--s2member-custom-reg-field-newsletters" aria-required="true" tabindex="59" class="ws-plugin--s2member-custom-reg-field s2conditional1"> <label for="ws-plugin--s2member-custom-reg-field-newsletters" style="display:inline !important; margin:0 !important;">Do you wish to receive JETAAUK Newsletters?</label></p>
    
    <p>
<label for="ws-plugin--s2member-custom-reg-field-jobalerts">
<span>Do you wish to receive job alerts (digest)? *</span></label><br>
<input type="radio" value="weekly" name="ws_plugin__s2member_custom_reg_field_jobalerts" id="ws-plugin--s2member-custom-reg-field-jobalerts---0" aria-required="true" tabindex="60" class="ws-plugin--s2member-custom-reg-field s2conditional2" style="color:#006699;"> <label for="ws-plugin--s2member-custom-reg-field-jobalerts-0" class="ws-plugin--s2member-custom-reg-field-op-l" style="display:inline !important; margin:0 !important;">weekly</label>&nbsp;&nbsp;<input type="radio" value="daily" name="ws_plugin__s2member_custom_reg_field_jobalerts" id="ws-plugin--s2member-custom-reg-field-jobalerts---1" aria-required="true" tabindex="60" class="ws-plugin--s2member-custom-reg-field s2conditional2" style="color:#006699;"> <label for="ws-plugin--s2member-custom-reg-field-jobalerts-1" class="ws-plugin--s2member-custom-reg-field-op-l" style="display:inline !important; margin:0 !important;">daily</label>&nbsp;&nbsp;<input type="radio" value="none" name="ws_plugin__s2member_custom_reg_field_jobalerts" id="ws-plugin--s2member-custom-reg-field-jobalerts---2" aria-required="true" tabindex="60" class="ws-plugin--s2member-custom-reg-field s2conditional2" style="color:#006699;"> <label for="ws-plugin--s2member-custom-reg-field-jobalerts-2" class="ws-plugin--s2member-custom-reg-field-op-l" style="display:inline !important; margin:0 !important;">none</label></p>
    
    <p>
<label for="ws-plugin--s2member-custom-reg-field-opt-in">
<input type="checkbox" name="ws_plugin__s2member_custom_reg_field_opt_in" id="ws-plugin--s2member-custom-reg-field-opt-in" class="ws-plugin--s2member-custom-reg-field" value="1" checked="not" tabindex="70">
<span style="color:#be0026;"><strong>=&gt; Yes, I wish to receive JETAAUK Newsletters via email &lt;= </strong></span></label></p>

答案 1 :(得分:0)

为了隐藏两个额外的表单部分,您需要使用JavaScript。我个人使用jQuery插件,从你对Mosh Feu的回答的评论来看,这不会是一个问题。

代码不是最佳代码,但由于您无法更改HTML,我们将使用我们的代码。

// Store the checkbox we'll be monitoring for changes
var checkTrigger = $('#ws-plugin--s2member-custom-reg-field-opt-in');

// Store the items we want to show/hide
// I store the 'none' radio button here - if the HTML changes, this will break
// we use this particular radio button further down
var jobAlerts = $('#ws-plugin--s2member-custom-reg-field-jobalerts---2');
var newsletterCheck = $('#ws-plugin--s2member-custom-reg-field-newsletters');

// Check for the change of the checkbox
checkTrigger.change(function() {
  toggleForm(checkTrigger.is(':checked'));
});

// Toggle the form
function toggleForm(isChecked) {
  jobAlerts.parent().toggle(isChecked);
  newsletterCheck.parent().toggle(isChecked);

  // We'll uncheck the red Newsletter checkbox too, if it's being hidden
  // and we'll set the job alerts to none;
  if (!isChecked) {
    newsletterCheck.prop("checked", false);
    jobAlerts.prop("checked", true);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
  <label for="ws-plugin--s2member-custom-reg-field-newsletters">
    <span style="display:none;">Do you wish to receive JETAAUK Newsletters? *</span>

  </label>
  <input type="checkbox" value="1" checked="checked" name="ws_plugin__s2member_custom_reg_field_newsletters" id="ws-plugin--s2member-custom-reg-field-newsletters" aria-required="true" tabindex="59" class="ws-plugin--s2member-custom-reg-field s2conditional1">
  <label for="ws-plugin--s2member-custom-reg-field-newsletters" style="display:inline !important; margin:0 !important;">Do you wish to receive JETAAUK Newsletters?</label>
</p>
<p>
  <label for="ws-plugin--s2member-custom-reg-field-jobalerts">
    <span>Do you wish to receive job alerts (digest)? *</span>

  </label>
  <br>
  <input type="radio" value="weekly" name="ws_plugin__s2member_custom_reg_field_jobalerts" id="ws-plugin--s2member-custom-reg-field-jobalerts---0" aria-required="true" tabindex="60" class="ws-plugin--s2member-custom-reg-field s2conditional2" style="color:#006699;">
  <label for="ws-plugin--s2member-custom-reg-field-jobalerts-0" class="ws-plugin--s2member-custom-reg-field-op-l" style="display:inline !important; margin:0 !important;">weekly</label>&nbsp;&nbsp;
  <input type="radio" value="daily" name="ws_plugin__s2member_custom_reg_field_jobalerts" id="ws-plugin--s2member-custom-reg-field-jobalerts---1" aria-required="true" tabindex="60" class="ws-plugin--s2member-custom-reg-field s2conditional2" style="color:#006699;">
  <label for="ws-plugin--s2member-custom-reg-field-jobalerts-1" class="ws-plugin--s2member-custom-reg-field-op-l" style="display:inline !important; margin:0 !important;">daily</label>&nbsp;&nbsp;
  <input type="radio" value="none" name="ws_plugin__s2member_custom_reg_field_jobalerts" id="ws-plugin--s2member-custom-reg-field-jobalerts---2" aria-required="true" tabindex="60" class="ws-plugin--s2member-custom-reg-field s2conditional2" style="color:#006699;">
  <label for="ws-plugin--s2member-custom-reg-field-jobalerts-2" class="ws-plugin--s2member-custom-reg-field-op-l" style="display:inline !important; margin:0 !important;">none</label>
</p>
<p>
  <label for="ws-plugin--s2member-custom-reg-field-opt-in">
    <input type="checkbox" name="ws_plugin__s2member_custom_reg_field_opt_in" id="ws-plugin--s2member-custom-reg-field-opt-in" class="ws-plugin--s2member-custom-reg-field" value="1" checked="not" tabindex="70">
    <span style="color:#be0026;"><strong>=&gt; Yes, I wish to receive JETAAUK Newsletters via email &lt;= </strong></span>

  </label>
</p>

jQuery代码假定HTML不会改变。如果复选框的ID值发生变化,那么jQuery将会中断。

我知道你不想在这里更改HTML,但是从UI的角度来看,底部的复选框隐藏了它上面的那些是没有意义的。用户从左到右,从上到下阅读,底部的复选框与其上方的元素进行交互,这个一般规则被打破。

您应该考虑将触发复选框移到顶部。