【问题标题】:How to send the selected option from a select_tag to an onchange javascript function如何将选定选项从 select_tag 发送到 onchange javascript 函数
【发布时间】:2023-04-06 07:45:01
【问题描述】:

我有一个 select_tag,它在更改时会使用 onchange 调用 javascript 方法。

我希望将在 select_tag 中选择的选项的值作为参数发送给方法。

<%= link_to_function select_tag :search_type, options_for_select(search_type_options, @search_type), :onchange => "showSearchables( **the option selected ** );return false;" %>

【问题讨论】:

    标签: javascript ruby-on-rails onchange html-select


    【解决方案1】:

    不使用 JQuery,您可以将选择的选项更改为:

    this.options[this.selectedIndex].value
    

    【讨论】:

      【解决方案2】:

      如果使用JQuery,可以使用

      :onchange => "showSearchables($(this).val());"
      

      【讨论】:

        【解决方案3】:

        就 HTML/JavaScript 而言,这非常简单:

        <form>
          <select onChange="getVal( this )">
            <option>one</option>
            <option>two</option>
            <option>three</option>
          </select>
        </form>
        
        <script type="text/javascript">
          function getVal( oSelect ) 
          {
            var selectedValue = oSelect.options[ oSelect.selectedIndex ].value;
          }
        </script>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-03-22
          • 2021-09-24
          • 2019-01-15
          • 1970-01-01
          • 2019-09-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多