【问题标题】:Change multiple INPUT values with multiple SELECTS [closed]使用多个 SELECTS 更改多个 INPUT 值 [关闭]
【发布时间】:2015-01-30 22:49:35
【问题描述】:

也许有人可以帮助我...我有 5 种不同的选择。我想在更改 SELECT 值后更改 INPUT 的值。但是 5 个选择和 5 个输入。每个选择有 1 个输入。

像这样:

<select name="select[0]>   // on change of select[0]
<input type="text" name="change_me[0]>  // put the value here

<select name="select[1]>   // on change of select[1]
<input type="text" name="change_me[1]>  // put the value here

<select name="select[2]>   // on change of select[2]
<input type="text" name="change_me[2]>  // put the value here

等等..目前我正在使用这个javascript代码:

$("#select").change(function() {
$(".change_me").val($(this).val());
});

但问题是,如何将它用于每对选择和输入字段。我必须写5次脚本吗?

谢谢你们!!

【问题讨论】:

  • 什么是&lt;input type="select"&gt;?不应该是&lt;select&gt;吗?
  • 当然,我的错误:) 谢谢

标签: javascript jquery ajax select input


【解决方案1】:

没有选择类型的输入元素。但是如果你需要当前元素将值复制到下一个元素,你可以这样做:

$(".current_element").change(function() {
    $(this).next().val($(this).val());
});

Fiddle Example

【讨论】:

  • 这正是我想要的!非常感谢!
  • next() 在我使用 DIV 时似乎不起作用。如果表单元素位于单独的 DIV 中,您知道如何处理吗? &lt;div&gt; &lt;select name="modello" class="select_option"&gt; &lt;option&gt;a&lt;/option&gt; &lt;option&gt;b&lt;/option&gt; &lt;/select&gt; &lt;/div&gt; &lt;div&gt; &lt;input type="text" name=""&gt; &lt;/div&gt; 我用 parent() 尝试了一些方法,但没有找到好的解决方案。
  • 你应该尝试使用 parent() 而不是 next() 而不是 children() 来解决它
  • 非常感谢。它是:$(this).parent().next().children().val($(this).val());
猜你喜欢
  • 1970-01-01
  • 2015-01-22
  • 2014-07-14
  • 2012-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-21
相关资源
最近更新 更多