简体   繁体   English

如何使用jQuery应用条件逻辑

[英]How to apply conditional logic using jquery

I have customized some coding in my site 我在网站上自定义了一些编码

There is a select field which has options: 1) singaporean 2) singapore PR 3) others 有一个选择字段,其中包含以下选项:1)新加坡2)新加坡PR 3)其他

once user select others, a text field will appear below for the user to key in any nationality user want to key in 一旦用户选择其他人,下面将出现一个文本字段,供用户键入要键入的任何国籍的用户

The problem here is that I have managed to get the text field to appear but after press "save settings" button, the text field disappears but the data is saved. 这里的问题是我设法使文本字段出现,但是在按下“保存设置”按钮之后,文本字段消失了,但是数据被保存了。

<li class="jobsearch-column-6">
  <label><?php esc_html_e('Nationality', 'wp-jobsearch') ?></label>
  <div class="jobsearch-profile-select">
    <label for="NationalitySelect">
      <select name="nationality" id="NationalitySelect" class="selectize-select" placeholder="<?php esc_html_e('Nationality', 'wp-jobsearch') ?>">
    </label>
    <option <?php echo ($nationality=='Singaporean' ? 'selected="selected"' : '') ?>value="Singaporean">
      <?php esc_html_e('Singaporean', 'wp-jobsearch') ?>
    </option>
    <option <?php echo ($nationality=='Singaporean PR' ? 'selected="selected"' : '') ?> value="Singaporean PR">
      <?php  esc_html_e('Singaporean PR', 'wp-jobsearch') ?>
    </option>
    <option <?php echo ($nationality=='Others' ? 'selected="selected"' : '') ?>value="Others">
      <?php esc_html_e('Others', 'wp-jobsearch') ?>
    </option>
    </select>
    <input type="text" style="display:none;" name="others_nationality" class="form-control" id="OthersNationalityInput" value="<?php echo ($others_nationality) ?>" />
  </div>
</li>
<script type="text/javascript">
  jQuery(document).on('change', '#NationalitySelect', function() {
    var nationalothers = $('#OthersNationalityInput');
    if (this.value == 'Others') {
      nationalothers.show();
    } else {
      nationalothers.hide();
    }
  });
</script>

I want the text field to remain shown after the button "save settings" is pressed. 我希望在按下“保存设置”按钮后文本字段保持显示。

When you submit the page get refreshed so it disappears. 提交页面时,页面会刷新,因此消失。 Try to add the textbox by PHP on form submit 尝试在表单提交时通过PHP添加文本框

if(isset($_POST['submit']))
{ 
  echo"<input type='text' val='value from database'/>";
}

Assuming after you submit then the page refreshed and the values in your form are maintained, you can modify your jQuery to this: 假设提交后刷新页面并维护表单中的值,则可以将jQuery修改为:

<script type="text/javascript">
  jQuery(function() {
    var nationalothers = $('#OthersNationalityInput');
    if (this.value == 'Others') {
      nationalothers.show();
    } else {
      nationalothers.hide();
    }
  });

  jQuery(document).on('change', '#NationalitySelect', function() {
    var nationalothers = $('#OthersNationalityInput');
    if (this.value == 'Others') {
      nationalothers.show();
    } else {
      nationalothers.hide();
    }
  });
</script>

But of course you should optimize the code above by keeping things DRY. 但是当然您应该通过保持DRY来优化上面的代码。

you can do it by changing this line 您可以通过更改此行来完成

<input type="text" style="display:none;" name="others_nationality" class="form-control" id="OthersNationalityInput" value="<?php echo ($others_nationality) ?>" />

to

<input type="text" style="display:<?php echo ($nationality=='Others' ? 'block' : 'none'); ?>" name="others_nationality" class="form-control" id="OthersNationalityInput" value="<?php echo ($others_nationality) ?>" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM