使用 jQuery 获取由变量定义的文本输入的值

Get value of an text input defined by variable using jQuery

本文关键字:文本 输入 定义 变量 jQuery 获取 使用      更新时间:2023-09-26

我正在尝试使用

var $rel = '#'+$(this).attr('rel');

但是当我测试它时它什么也没返回

alert($rel.val());

示例:http://jsfiddle.net/59VmX/

'#'+$(this).attr('rel');  // This is just a string

应该是

$('#'+$(this).attr('rel'));  OR // $('#'+ this.rel);
如果要

在对象上使用val方法,则需要将其包装为jQuery对象

删除.val()尝试alert($rel);

这里有一个工作小提琴,在你的旧小提琴中,jquery没有加载并且:

取而代之的是:

 var $rel = '#'+$(this).attr('rel');
 alert($rel.val());

它应该是:

 var $rel = $(this).attr('rel');
 alert($rel);