0

我在我的一个脚本中使用了该脚本,这很棒,我以为我解决了问题,但是每当我更改选项时,页面上的每个按钮都会更改为相同的选项值。我可以将我的值限制为单一输入类型吗?这是有问题的脚本

<select>
  <option text="ok">1</option>
  <option text="hello">2</option>
  <option text="test">3</option>
</select>
<input type="text" />

$(function(){
 $('select').on('change', function(){
    $('input').val($('option:selected', this).attr('text'));
  });
});
4

3 回答 3

1

当然,只需将 anid应用于特定的输入并更改

$('input').val(...)

进入

$('#your-input-id').val(...)
于 2013-04-11T08:02:55.483 回答
0

这就是为什么:

$('input')

您选择的每一次更改,都是针对页面中的所有输入。您可以通过使选择器特定来进行限制,例如:

$('input[type=text]')   //all inputs of type "text"
$('input[type=submit]') //all inputs of type "submit"
$('input.customButton') //all inputs of class "customButton"
于 2013-04-11T08:04:00.487 回答
0

如果每个SELECT菜单INPUT下方都有您要更新的菜单,您可以执行以下操作:

$("select").change(function() {
    $(this).next("input[type='text']").val($('option:selected', this).attr('text'));
});
于 2013-04-11T08:08:31.607 回答