简体   繁体   English

显示保存的下拉值而不是占位符

[英]Showing saved drop down value instead of placeholder

HTML to render field: HTML呈现字段:

<p class="form-row form-row-wide">
                <label for="reg_diocese">Diocese</label>
                <select type="text" class="input-text" name="diocese" id="diocese" value="<?php echo esc_attr( $user->diocese ); ?>" class="regular-text" />
                     <option value="<?php echo esc_attr( $user->diocese ); ?>"><?php echo esc_attr( $user->diocese ); ?></option>   
 </select>
                </p>

Updated -->The above code is shown on the register form, which saves the selected diocese value from the drop down as new user meta within the user table. 更新 ->上面的代码显示在注册表单上,该表单将下拉列表中选定的教区值保存为用户表中的新用户元。

Js : Js:

   var jsonData = {
  "Table": [{
      "diocese": "Aberdeen & Orkney"
      }, {
          "diocese": "Armagh"
      }, {
          "diocese": "Bangor"
      }, {
          "diocese": "Bath & Wells"
      }, {
          "diocese": "Birmingham"
      }, {



  }]
  };



     var listItems = '<option selected="selected" value="0">Select</option>';

  for (var i = 0; i < jsonData.Table.length; i++) {
         listItems += "<option value='" + jsonData.Table[i].diocese + "'>" + jsonData.Table[i].diocese + "</option>";
     }

     $("#diocese").html(listItems);

edit-account.php: 编辑account.php:

        <p class="form-row form-row-wide">
            <label for="reg_diocese">Diocese</label>
            <select onchange="<?php echo esc_attr( $user->diocese ); ?>" type="text" class="input-text" name="diocese" id="diocese" value="<?php echo esc_attr( $user->diocese ); ?>" class="regular-text" />
                 <option value="<?php echo esc_attr( $user->diocese ); ?>"><?php echo esc_attr( $user->diocese ); ?></option>
            </select>
            </p>

Updated --> The user has the option to edit their registration options, all the standard text fields are showing the value brought forward by the database but the select field isn't showing the selected value it's instead showing 'Select' which is set in the js... 更新 ->用户可以选择编辑注册选项,所有标准文本字段都显示数据库提出的值,但是选择字段不显示所选值,而是显示在其中设置的“选择” js ...

So the value is currently select within the form and saved to the database via user meta and which allows me to show it across the site on the front end. 因此,当前在表单中选择该值,并通过用户meta将其保存到数据库中,这使我可以在前端的整个站点中显示该值。

The issue is the placeholder of the select field is shown within edit-form.php where I actually want the value that the user has selected to show. 问题是select字段的占位符显示在edit-form.php中,我实际上希望用户选择显示的值。

If I inspect the field, the value has actually updated to my selection, just the placeholder is not changing... 如果我检查该字段,则该值实际上已更新为我的选择,只是占位符没有改变...

Thanks, Dan 谢谢,丹

You should adapt your code with Select2 . 您应该使用Select2修改代码。 You can set value with jQuery lazily. 您可以使用jQuery懒惰地设置值。

$("#id").val("Some choice");

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

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