2011-11-24 76 views
1

大家好我試着當用戶使用jQuery點擊文本框獲取文本框的值,但是當我用警報()看到的值這裏是我的了:jquery - 如何從文本框中獲取值?

function (value) { 
var hooks, ret, elem = this[0]; 
if (!arguments.length) { 
    if (elem) { 
     hooks = jQuery.valHooks[elem.nodeName.toLowerCase()] || 
      jQuery.valHooks[elem.type]; 
     if (hooks && 
      "get" in hooks && 
      (ret = hooks.get(elem, "value")) !== undefined) { 
      return ret; 
     } 
     ret = elem.value; 
     return typeof ret === "string" ? ret.replace(rreturn, "") : ret == null ? "" : ret; 
    } 
    return undefined; 
} 
var isFunction = jQuery.isFunction(value); 
return this.each(function (i) {var self = jQuery(this), val;if (this.nodeType !== 1) {return;}if (isFunction) {val = value.call(this, i, self.val());} else {val = value;}if (val == null) {val = "";} else if (typeof val === "number") {val += "";} else if (jQuery.isArray(val)) {val = jQuery.map(val, function (value) {return value == null ? "" : value + "";});}hooks = jQuery.valHooks[this.nodeName.toLowerCase()] || jQuery.valHooks[this.type];if (!hooks || !("set" in hooks) || hooks.set(this, val, "value") === undefined) {this.value = val;}});} 

這裏是我的代碼:http://jsfiddle.net/QYHXu/1/

請幫忙。提前致謝。

回答

9
$(this).val 

...是函數。

$(this).val() 

...是該函數的返回值。使用$(this).val()

+0

哦,我明白了......非常感謝。 – NinjaBoy

3

更改您val功能

$(this).val); 

$(this).val()); 
+0

他的jsFiddle更多的是展示他的問題,而不是展示一個用例的例子。 –

+0

@Jim H哦,明白了。多謝,夥計 –

1

試試這個

$(function(){ 
    $('input#txtName').click(function() { 
     alert(document.getElementById('txtName').value); 
    }); 
}); 
3

您忘記了Val函數括號花花公子:)