显示选择标记的部分值的Java脚本


Java script to display partial value of the select tag

我是很新的javascript,但我想写一个脚本的选择标签(为国家和他们的电话代码)。我希望这样的下拉列表显示国家和代码,但在用户选择后,应该只显示代码。我用PHP来写网页。由于

我认为最适合你的是使用JQuery方法,如下面的代码。

你的HTML应该像下面这样。

<select>
  <option value="01" textValue="America">01</option>
  <option value="02" textValue="Liberia">02</option>
  <option value="03" textValue="Switzerland">03</option>
</select>
$("your select").on('focus', function(){
    $(this).children().each(function(){
        var text = $(this).attr('textValue') + ' - ' + $(this).attr('value');
        $(this).text(text);
    });
});
$("your select").on('blur', function(){
    $(this).children().each(function(){
        $(this).text($(this).attr('value'));
    });
});

我想你应该是这样的

<select>
  <option value="01">America - 01</option>
  <option value="02">Liberia - 02</option>
  <option value="03">Switzerland - 03</option>
</select>

你可以在javascript中使用click event来选择,不太确定你打算如何使用那些select.