通过选择带变量的名称来设置下拉列表

时间:2014-10-12 16:20:57

标签: jquery

我正在尝试使用select name = + variable +中的变量设置下拉列表的焦点 我的警报显示要关注的选择的正确名称,但焦点未设置。如果我使用select的实际名称而不是变量,则焦点起作用。我看了几个例子,尝试了一下“围绕这个名字,但它仍然不会集中注意力。”谢谢你们。

CSS

<style>
select:focus {
   background-color: yellow;
 }
</style> 

JQuery的

<script>

$('input[name^=dialsetting]').click(function () {

      var name = $(this).attr('name');
      var headtypenum = name.replace('dialsetting', 'headtype');

      alert(headtypenum);
      $('select[name=+headtypenum+]').focus();


    });
</script>

HTML

<select name="headtype1">
    <option id="item37_0_option" selected value="1">Choose one</option>
    <option id="item37_1_option" value="A3">A3</option>
</select>
<br>
<br>
<select name="headtype2">
    <option selected value="1">Choose one</option>
    <option value="A4">A4</option>
</select>
<br>
<br>
<input name="dialsetting1" required type="number" />
<br>
<br>
<input name="dialsetting2" required type="number" />

这是我的小提琴 http://jsfiddle.net/progrower/3k4jv275/1/

1 个答案:

答案 0 :(得分:2)

你选择器错了。将其更改为:

$('select[name=' + headtypenum + ']').focus();

Demo