jQuery属性等于选择器不起作用

时间:2014-10-16 06:05:08

标签: javascript jquery jquery-selectors jquery-attribute-equals

jQuery 属性等于选择器无效。请看一下。 提前致谢。 :)



<!DOCTYPE html>
<html>
	<head>
	<title>My Page</title>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css" />
	<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
	<script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>
	<script type="text/javascript" >
		
	$( document ).ready(function() {
    	alert($( "input[myattr='navo']" ).value);
		$( "input[myattr='navo']" ).value="Simon Commission";
		alert($( "input[myattr='navo']" ).value);
	});
   </script>
</head>
<body>
	<input type="text" myattr='navo' value="Morley Minto Reform" />
</body>
</html>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:5)

试试这个,

alert($( "input[myattr='navo']" ).val());

在jQuery值中可以使用val()方法得到它,它没有像value这样的属性。

答案 1 :(得分:2)

$( "input[myattr='navo']" )返回一个jQuery对象,而不是dom元素,因此它没有名为value的属性。您需要在jQuery对象上使用jQuery提供的各种方法。

在这种情况下,您可以使用.val()方法来获取/设置输入元素的值

$(document).ready(function() {
  alert($("input[myattr='navo']").val());
  $("input[myattr='navo']").val("Simon Commission");
  alert($("input[myattr='navo']").val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" myattr='navo' value="Morley Minto Reform" />

答案 2 :(得分:2)

jQuery对象上不存在.value属性。如果要访问该值,则必须调用.val()函数;如果要更改值,则必须将新值传递给.val("some value")函数。

&#13;
&#13;
<!DOCTYPE html>
<html>
	<head>
	<title>My Page</title>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css" />
	<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
	<script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>
	<script type="text/javascript" >
		
	$( document ).ready(function() {
    	alert($( "input[myattr='navo']" ).val());
		$( "input[myattr='navo']" ).val("Simon Commission");
		alert($( "input[myattr='navo']" ).val());
	});
   </script>
</head>
<body>
	<input type="text" myattr='navo' value="Morley Minto Reform" />
</body>
</html>
&#13;
&#13;
&#13;