首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >使用jquery keyup获取不同输入字段的值

使用jquery keyup获取不同输入字段的值
EN

Stack Overflow用户
提问于 2015-04-12 02:17:08
回答 1查看 2.6K关注 0票数 0

我有一个有4个输入字段的表单

代码语言:javascript
代码运行次数:0
运行
复制
<div id="st5_one">
Top <input class="tb2" style="width:100px;" type="text" maxlength="3"   name="margin_top" placeholder="Margin top" value="5"> % 
Right <input class="tb2" style="width:100px;" type="text" maxlength="3" name="margin_right" placeholder="Margin right" value="5"> % 
Bottom <input class="tb2" style="width:100px;" type="text" maxlength="3" name="margin_bottom" placeholder="Margin bottom" value="5"> % 
Left <input class="tb2" style="width:100px;" type="text" maxlength="3" name="margin_left" placeholder="Margin left" value="5"> %
</div>

在我的代码中,当值在任何字段中更改时,我都试图从所有字段中获取值。

代码语言:javascript
代码运行次数:0
运行
复制
jQuery('input[name=margin_right]') .keyup(function(){
   //get the value in the input box
   var vl = jQuery(this).val();
   var image_right = (parseInt(vl))
   var image_top = $('#margin_top').val(); 
   alert("top is " + image_top)
   var image_top = jQuery('input[name=margin_top]').val();
   alert("top is " + image_top)
});

但是,对于image_top值,我总是没有定义。我正在获取margin_right字段中输入的值。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-12 02:41:28

这是弹琴的工作装置。您指定了错误的目标元素。您使用ID作为目标元素,但是输入没有ID

代码语言:javascript
代码运行次数:0
运行
复制
https://jsfiddle.net/c8ms2ugo/

示例代码

HTML

代码语言:javascript
代码运行次数:0
运行
复制
<div id="st5_one">
Top 
<input id="top-input" class="tb2" style="width:100px;" type="text" maxlength="3" name="margin_top" placeholder="margin top">%

Right 
<input id="right-input" class="tb2" style="width:100px;" type="text" maxlength="3" name="margin_right" placeholder="margin right" value="5">%

Bottom <input id="bottom-input" class="tb2" style="width:100px;" type="text" maxlength="3" name="margin_bottom" placeholder="margin bottom" value="5">%

Left <input id="left-input" class="tb2" style="width:100px;" type="text" maxlength="3" name="margin_left" placeholder="margin left" value="5">%
</div>

Javascript

代码语言:javascript
代码运行次数:0
运行
复制
jQuery('input[name=margin_right]') .keyup(function(){
   //get the value in the input box
   var vl = jQuery(this).val();
   var image_right = (parseInt(vl))

   var image_top = $('#top-input').val(); 
   alert('Top is ' + image_top)

   var image_bottom = $('#bottom-input').val(); 
    alert('Bottom is ' + image_bottom)

       var image_left = $('#left-input').val(); 
    alert('Left is ' + image_left)
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29585229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档