Jquery 表单提交失败后显示隐藏字段

Jquery 表单提交失败后显示隐藏字段,jquery,Jquery,我对jQuery比较陌生。我有一个表单(),如果用户选择“其他”,将显示两个字段 如果用户提交表单,并且表单返回错误(表单验证是用PHP处理的),如何获取隐藏字段以显示用户是否选择了“其他” 下面是我正在使用的jquery: $(document).ready(function() { $('.location-other').hide(); $('.location').change(function(){ if($('.location op

我对jQuery比较陌生。我有一个表单(),如果用户选择“其他”,将显示两个字段

如果用户提交表单,并且表单返回错误(表单验证是用PHP处理的),如何获取隐藏字段以显示用户是否选择了“其他”

下面是我正在使用的jquery:

    $(document).ready(function() {

      $('.location-other').hide();
      $('.location').change(function(){
        if($('.location option:selected').val() == 'other') {
          $('.location-other').show();
        } else {
          $('.location-other').hide();
        }
      });

    });
HTML表单预提交:

<form>

  <h2>Location</h2>

  <div class="field">
    <label for="business_id" class="label">Venue</label>
    <div class="select">
      <select name="business_id" class="location">
        <option value="3">Location 1</option>
        <option value="7">Location 2</option>
        <option value="other">Other</option>
      </select>

    </div>

    <div class="location-other">

      <p>
        <label for="event_location_name" class="label">Venue name</label>
        <input type="text" class="input" id="event_location_name" name="event_location_name" value="" />
      </p>

      <p>
        <label for="event_location_address" class="label">Address</label>
        <input type="text" class="input" id="event_location_address" name="event_location_address" value="" />
      </p>

    </div>

  </div>
  <p class="field">
    <button name="event_insert" class="button is-primary">Save and continue &rarr;</button>
  </p>

</form>

位置
地点
地点1
地点2
其他

场馆名称

地址

保存并继续&rarr;

提交后的HTML表单:

<form>

  <h2>Location</h2>

  <div class="field">
    <label for="business_id" class="label">Venue</label>
    <div class="select">
      <select name="business_id" class="location">
        <option value="3">Location 1</option>
        <option value="7">Location 2</option>
        <option value="other" selected>Other</option>
      </select>

    </div>

    <div class="location-other">

      <p>
        <label for="event_location_name" class="label">Venue name</label>
        <input type="text" class="input" id="event_location_name" name="event_location_name" value="Name" />
      </p>

      <p>
        <label for="event_location_address" class="label">Address</label>
        <input type="text" class="input" id="event_location_address" name="event_location_address" value="Address" />
      </p>

    </div>

  </div>
  <p class="field">
    <button name="event_insert" class="button is-primary">Save and continue &rarr;</button>
  </p>

</form>

位置
地点
地点1
地点2
其他

场馆名称

地址

保存并继续&rarr;


请参考下面的代码。也请检查评论中的小提琴链接。若表单中有错误,可以使用returnfalse防止按钮操作触发进一步的代码。所以您的表单状态将保持不变,并且如果您选择了其他选项,则它将仅将隐藏字段保持为可见状态。如果没有错误,则可以继续提交操作。我想你已经检查过错误了

    <form>

  <h2>Location</h2>

  <div class="field">
    <label for="business_id" class="label">Venue</label>
    <div class="select">
      <select name="business_id" class="location">
        <option value="3">Location 1</option>
        <option value="7">Location 2</option>
        <option value="other">Other</option>
      </select>

    </div>

    <div class="location-other">

      <p>
        <label for="event_location_name" class="label">Venue name</label>
        <input type="text" class="input" id="event_location_name" name="event_location_name" value="" />
      </p>

      <p>
        <label for="event_location_address" class="label">Address</label>
        <input type="text" class="input" id="event_location_address" name="event_location_address" value="" />
      </p>

    </div>

  </div>
  <p class="field">
    <button id="saveData" name="event_insert" class="button is-primary"> Save and continue →</button>
  </p>

</form>

    $(document).ready(function() {

      $('.location-other').hide();
      $('.location').change(function(){
        if($('.location option:selected').val() == 'other') {
          $('.location-other').show();
        } else {
          $('.location-other').hide();
        }
      });     
      $( "#saveData" ).click(function() {
        var isError = true;
        if(isError){
            return false
        }
        else{
            alert("submitted");
        }
      });

    });

位置
地点
地点1
地点2
其他

场馆名称

地址

保存并继续→

$(文档).ready(函数(){ $('.location-other').hide(); $('.location').change(函数(){ if($('.location选项:选中').val()=='other'){ $('.location-other').show(); }否则{ $('.location-other').hide(); } }); $(“#保存数据”)。单击(函数(){ var-isError=true; 如果(isError){ 返回错误 } 否则{ 警报(“已提交”); } }); });
请记住以以下形式打印所选选项:

<option value="other" selected>Other</option>

请编辑您的问题并添加PHP代码alsouse ajax call以提交表单数据,然后使用response显示值请将所有相关信息包含在问题本身中,最好作为一个示例。你的HTML在这里很重要,不要让我们离开网站去寻找关键信息。让我们尽可能容易地帮助您。请显示您的PHP代码进行验证。以上答案请点击链接
$(document).ready(function() {
    check_location();

    $('.location').change(check_location);
});

function check_location() {
    if($('.location option:selected').val() == 'other') {
        $('.location-other').show();
    } else {
        $('.location-other').hide();
    }
}