material-components-web icon indicating copy to clipboard operation
material-components-web copied to clipboard

[MDC Select] Example in README does send values to the web server

Open mikail-khan opened this issue 7 years ago • 3 comments

I took this example code from your README

                        <div class="mdc-select mdc-select--outlined role-list">
                            <div class="mdc-select__anchor">
                                <i class="mdc-select__dropdown-icon"></i>
                                <div id="demo-selected-text" class="mdc-select__selected-text" aria-labelledby="outlined-select-label"></div>
                                <div class="mdc-notched-outline">
                                    <div class="mdc-notched-outline__leading"></div>
                                    <div class="mdc-notched-outline__notch">
                                        <label id="outlined-select-label" class="mdc-floating-label">Pick a role</label>
                                    </div>
                                    <div class="mdc-notched-outline__trailing"></div>
                                </div>
                            </div>

                            <!-- Other elements from the select remain. -->
                            <div class="mdc-select__menu mdc-menu mdc-menu-surface" role="listbox" name="role">
                                <ul class="mdc-list">
                                    <li class="mdc-list-item" data-value="0">
                                        Chief Compliance Officer
                                    </li>
                                </ul>
                            </div>
                        </div>

However, when I post the form containing this component, I fail to see the value being submitted to the web server. I have other MDC components such text fields and I see the values being posted to the web server but not for this component. I have no idea why. I have asked on your Discord channel but no response and I have also asked on StackExchange but no response. I am hoping I can get some help here. Thanks

mikail-khan avatar Dec 02 '19 23:12 mikail-khan

Hi, standart form tag submit only these tags:

<button>
<fieldset>
<input>
<object>
<output>
<select>
<textarea>

https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/elements

but in version 4.0 the usual select and option were replaced.

p.s. sorry bad english

cryol avatar Dec 22 '19 23:12 cryol

Copying the suggestion from the discord chat - credit to @asyncLiz

If you want its value to submit in a form, you may have better luck syncing its value to a hidden named input in the form.

Try something like this:

<div class="mdc-select">
  ...
</div>
<input id="pet-select" type="hidden" name="pets">
<script>
  const input = document.querySelector('#pet-select');
  const select = document.querySelector('.mdc-select');
  select.addEventListener('MDCSelect:change', e => {
    input.value = e.detail.value;
  });
</script>

daniel-dm avatar Jan 07 '20 23:01 daniel-dm

Closing this, please refer to #5428 for further tracking

bonniezhou avatar Jan 08 '20 17:01 bonniezhou