在下拉列表中更改选定的选项值

时间:2017-01-19 09:29:29

标签: javascript jquery html html5

我的下拉列表如下,

    <select name="speed" id="ddlCustomer" class="form-control select-basic">
    <optgroup label="CustomerId&nbsp;&nbsp;OrderDate&nbsp;&nbsp;&nbsp;&nbsp;SupplyDate &nbsp;&nbsp;&nbsp;&nbsp;Supplier">
    <option value="1011_2">1011&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2015-12-18 &nbsp;&nbsp;&nbsp;2015-12-22&nbsp;&nbsp;&nbsp;ABC</option>
        <option value="1011_2">1034&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2015-12-23 &nbsp;&nbsp;&nbsp;2015-12-28&nbsp;&nbsp;&nbsp;XYZ</option>
    </optgroup>
</select>

目前下拉列表显示“1011 2015-12-18 2015-12-22 ABC”等选项,这很好,但是当用户选择一个选项时,我只需要显示“CustomerId”,即1011。

非常感谢您的帮助。感谢

添加了关注焦点的脚本

customSelect.focusout(function () {
                                customSelectOptions.each(function (options) {
                                    if ($(this).is(':selected')) {

                                        $(this).text($(this).attr('value').split('_')[0]);
                                        $(this).blur();
                                    }
                                });



                            });

3 个答案:

答案 0 :(得分:2)

&#13; &#13; var states = [];&#13;     var customSelect = $(&#39; #ddlCustomer&#39;);&#13;     var customSelectOptions = customSelect.children()。children();&#13;     //获取每个状态,然后将它们推送到数组&#13;     //初始状态声明&#13;     customSelectOptions.each(function(){&#13;         var state = $(this).text();&#13;         states.push({state:state});&#13;         if($(this).is(&#39;:selected&#39;)){&#13;             $(本)的.text($(本).attr(&#39;值&#39;。)分裂(&#39; _&#39;)[0]);&#13;         }&#13;     });&#13; &#13;     //在焦点上,始终保留完整的州名&#13;     customSelect.on(&#39; focus&#39;,function(){&#13; &#13;         customSelectOptions.each(function(index){&#13;             $(本)的.text(状态[指数] .STATE);&#13;         });&#13; &#13;         //在更改时,将值附加到所选选项&#13;         $(this).on(&#39; change&#39;,function(){&#13; &#13;             customSelectOptions.each(function(options){&#13;                 if($(this).is(&#39;:selected&#39;)){&#13;                     $(本)的.text($(本).attr(&#39;值&#39;。)分裂(&#39; _&#39;)[0]);&#13;                 }&#13;             });&#13; &#13;             //无焦点选择完成&#13;             $(本).blur();&#13; &#13;         });&#13; &#13;     });&#13; &lt; script src =&#34; https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt; / script&gt;&#13; &lt; select name =&#34; speed&#34; ID =&#34; ddlCustomer&#34; class =&#34; form-control select-basic&#34;&gt;&#13;     &lt; optgroup label =&#34; CustomerId&amp; nbsp;&amp; nbsp; OrderDate&amp; nbsp;&amp; nbsp;&amp; nbsp;&amp; nbsp; SupplyDate&amp; nbsp;&amp; nbsp;&amp; nbsp;&amp; nbsp;供应商&#34;&GT;&#13;     &lt; option value =&#34; 1011_2&#34;&gt; 1011&amp; nbsp;&amp; nbsp;&amp; nbsp;&amp; nbsp;&amp; nbsp;&amp; nbsp;&amp; nbsp;&amp; nbsp;&amp; nbsp ;&amp; nbsp;&amp; nbsp; 2015-12-18&amp; nbsp;&amp; nbsp;&amp; nbsp; 2015-12-22&amp; nbsp;&amp; nbsp;&amp; nbsp; ABC&lt; / option&gt;&#13 ;         &lt; option value =&#34; 1034_2&#34;&gt; 1034&amp; nbsp;&amp; nbsp;&amp; nbsp;&amp; nbsp;&amp; nbsp;&amp; nbsp;&amp; nbsp;&amp; nbsp;&amp; nbsp ;&amp; nbsp;&amp; nbsp; 2015-12-23&amp; nbsp;&amp; nbsp;&amp; nbsp; 2015-12-28&amp; nbsp;&amp; nbsp;&amp; nbsp; XYZ&lt; / option&gt;&#13 ;     &LT; / OPTGROUP&GT;&#13; &LT; /选择&GT;&#13; &#13; &#13;

答案 1 :(得分:1)

尝试这样。您使用带有所选选项文本值的正则表达式。

    Jennyfer, I think you have to first do swipe logic in your listview.

    Bellow library to make the items in a ListView dismissable with the possibility to undo it, using your own view to providing this functionality like the Gmail app for Android does.    
for swipe to call you can refer bellow link.

 https://github.com/hudomju/android-swipe-to-dismiss-undo


    after that you can add calling functionality.like that

    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:0377778888"));
    startActivity(callIntent); 
   var ddlCustomer = document.querySelector('#ddlCustomer');
ddlCustomer.addEventListener('change',function(){
text = $(this).find("option:selected").text();
var value = text.match(/[0-9]+/);
$(this).find("option:selected").text(value);
alert(value);
});

请参见此处https://jsfiddle.net/88cozeaz/1/

答案 2 :(得分:0)

    document.getElementById('#ddlCustomer').onchange = function() {

      var option = this.options[this.selectedIndex];
      option.setAttribute('data-text', option.text);
      option.text =$(this).val();

      // Reset texts for all other but current
      for (var i = this.options.length; i--; ) {
        if (i == this.selectedIndex) continue;
        var text = this.options[i].getAttribute('data-text');
        if (text) this.options[i].text = text;
      }
    };