简体   繁体   English

jQuery约束比例与宽度和高度输入字段

[英]Jquery constraint proportions with width and height input fields

I have a form with width and height input fields. 我有一个带有宽度和高度输入字段的表单。 I want to listen to changes made in any one of the fields by the user and change the value in the other field wile constraint proportions using Jquery. 我想听用户在任何一个字段中所做的更改,并使用Jquery更改其他字段中的约束比例值。 the user can see the changes as he typs. 用户可以在键入时看到更改。

<form>
  <p>
    <label>Width</label><br />
    <input type="text" id="width" value="16" />
  </p>
  <p>
    <label>Height</label>
    <input type="text" id="height" value="9" />
  </p>
</form>

How about this. 这个怎么样。 I am storing the original value in data-value attribute, that I use to recount the ratio 我将原始值存储在data-value属性中,用于重新计算比率

<form>
   <p>
      <label>Width</label><br />
      <input type="text" id="width" data-value="16" value="16" />
  </p>
  <p>
      <label>Height</label>
      <input type="text" id="height" data-value="9" value="9" />
  </p>
</form>​​​​​​​​​

Here is the listener for changing width (you can write the one for height in the same way) 这是改变宽度的监听器(您可以用相同的方式写一个高度的监听器)

​$("#width").bind("keyup", function(){
   var width = $(this).data("value")
     , newwidth = $(this).val()
     , $height = $("#height")
     , height = $height.val();

   $height.val(height*(newwidth/width));

})​

Here is the fiddle 这是小提琴

尝试在.keypress(handler(eventObject))上附加一个事件,该事件对于所使用的每次按键都会执行您上面所说的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM