从另一个选择输入

时间:2012-05-06 18:52:25

标签: javascript jquery jquery-ui

我有这两行:

<tr>
  <td class="contenu"><input type="text" name="numart" id="numart"/></td>
  <td class="contenu"><input type="text" name="descart" id="descart"/></td>
  <td class="contenu"><input type="text" name="pdepart" id="pdepart"/></td>
  <td class="contenu"><input type="text" name="pvart" id="pvart"/></td>
  <td class="contenu"><input type="text" name="ageart" id="ageart"/></td>
  <td><a class="plus" title="Rajouter une ligne">+</a></td>
</tr>
<tr>
  <td class="contenu"><input type="text" name="numart" id="numart"/></td>
  <td class="contenu"><input type="text" name="descart" id="descart"/></td>
  <td class="contenu"><input type="text" name="pdepart" id="pdepart"/></td>
  <td class="contenu"><input type="text" name="pvart" id="pvart"/></td>
  <td class="contenu"><input type="text" name="ageart" id="ageart"/></td>
  <td><a class="plus" title="Rajouter une ligne">+</a></td>
</tr>

我如何选择:

<input type="text" name="pvart" id="pvart"/>

自:

<input type="text" name="pdepart" id="pdepart"/>

由于:

pvart= 2*pdepart

4 个答案:

答案 0 :(得分:0)

首先,您应该解决重复的ID以避免JavaScript的问题

如果您只想对一个输入中的更改做出反应,将其乘以2,然后插入另一个输入,那么您可以执行以下操作:

$("#pdepart").change(function() {
    $("#pvart").val(parseInt($(this).val(), 10) * 2);
});

编辑:如果你只想做一次:

 $("#pvart").val(parseInt($("#pdepart").val(), 10) * 2);

答案 1 :(得分:0)

请修改您的答案以获得正确的HTML。 HTML不允许两个具有相同id属性的元素。如果这是你的页面编码方式,那么你需要先解决这个问题。

如果您想预先填充,请使用javascript。

答案 2 :(得分:0)

如果认为正确填充pvart,请在事件上尝试以下代码行。 可能是pdepart的onkeyup事件或pdepart的onblur事件 添加了+图标将值转换为数字

document.getElementById('pvart').value = (+document.getElementById('pdepart').value)*2;

答案 3 :(得分:0)

$('#pdepart').each(function() {
   if(this.value)
       $(this).next('#pvart').val(parseInt(this.value) * 2);
});

或者,如果您想要更改任何事件,可以执行以下操作:

$('#pdepart').on('keyup', function() { //  you can bind any event like focus, change, blur etc
   if(this.value)
           $(this).next('#pvart').val(parseInt(this.value) * 2);
});

注意:

$(this).next('#pvart')会选择您正在处理的input#pvart旁边的input#pdepart