简体   繁体   English

触发自然语言形式的选择值

[英]Trigger select value of natural language form

I'm trying to implement a natural language form in Wordpress using the Contact Form 7 plugin and this CSS/JS from codrops. 我正在尝试使用Contact Form 7插件和codrops的CSS / JS在Wordpress中实现自然语言形式。

What it does is to hide the select elements and replace them with an <ul> , and the relative values generate the <li> childrens. 它的作用是隐藏选择元素,并将其替换为<ul> ,并且相对值生成<li>子元素。 Everything is fine, except that this particular script does apply a .cheched class to the <li> element that matches the selected "value", but the <select> element is not triggered. 一切都很好,只不过该特定脚本确实将.cheched类应用于与所选“值”匹配的<li>元素,但未触发<select>元素。 Is there a way to hack trigger the selected value? 有没有办法破解所选值?

I can only assume that this should be done within the "close" method: 我只能假设这应该在“关闭”方法中完成:

close : function( opt, idx ) {
          if( !this.open ) {
            return false;
          }
          this.open = false;
          this.form.fldOpen = -1;
          this.fld.className = this.fld.className.replace(/\b nl-field-open\b/,'');

          if( this.type === 'dropdown' ) {
            if( opt ) {
              // remove class nl-dd-checked from previous option
              var selectedopt = this.optionsList.children[ this.selectedIdx ];
              selectedopt.className = '';
              opt.className = 'nl-dd-checked';
              this.toggle.innerHTML = opt.innerHTML;
              $(this.elOriginal).change();
              // update selected index value
              this.selectedIdx = idx;
              // update original select element´s value
              this.elOriginal.value = this.elOriginal.children[ this.selectedIdx ].value;
            }
          } 
          else if( this.type === 'input' ) {
            this.getinput.blur();
            this.toggle.innerHTML = this.getinput.value.trim() !== '' ? this.getinput.value : this.getinput.getAttribute( 'placeholder' );
            this.elOriginal.value = this.getinput.value;
          }
        }

Following zensei suggestion I've tried to implement the code, but the select element doesn't change. 按照zensei的建议,我尝试实现代码,但是select元素没有改变。 Here's where I've put the jquery bit inside the close method: 这是将jquery位放在close方法中的位置:

if( this.type === 'dropdown' ) {
            if( opt ) {
              // remove class nl-dd-checked from previous option
              var selectedopt = this.optionsList.children[ this.selectedIdx ];
              selectedopt.className = '';
              opt.className = 'nl-dd-checked';
              this.toggle.innerHTML = opt.innerHTML;
              // update selected index value
              this.selectedIdx = idx;
              // update original select element´s value
              this.elOriginal.value = this.elOriginal.children[ this.selectedIdx ].value;
              jQuery(this.elOriginal).trigger('select');
            }
          }

It just so happens that I was just looking for a similar solution using the natural language form from codrops. 碰巧的是,我只是想用codrops的自然语言形式寻求类似的解决方案。 I found an answer utilizing jquery in the comments section of the page where the original code is available. 我在页面的注释部分找到了利用jquery的答案,该页面的原始代码可用。 It was posted by a user named Jason. 它由一个名为Jason的用户发布。

https://tympanus.net/codrops/2013/05/21/natural-language-form-with-custom-input-elements/ https://tympanus.net/codrops/2013/05/21/natural-language-form-with-custom-input-elements/

In the close function, just after updating the original select element's value, you can insert the following code to trigger the selecting of the original element: 在关闭功能中,在更新原始选择元素的值之后,您可以插入以下代码来触发对原始元素的选择:

jQuery(this.elOriginal).trigger('select'); jQuery(this.elOriginal).trigger('select');

I hope this helps. 我希望这有帮助。

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

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