[英]Get value of input in jQuery
I want to redirect to another page when a checkbox is clicked.单击复选框时,我想重定向到另一个页面。
<script type="text/javascript"><!--
$(document).ready(function(){
jQuery(".cbno").click(function(e){
e.preventDefault();
alert('test');
alert(this.val());
// window.location = this.val();
});
});
//-->
</script>
<input type="checkbox" class="cbno" name="content" value="/map/?filter=all" />
Pretty simple - but I cannot figure out why the second alert does not produce any output?很简单——但我不明白为什么第二个警报不产生任何 output? Inte.net Explorer says: "The object does not support the method val".
Inte.net Explorer 说:“object 不支持方法 val”。
It works if I use this.getAttribute('value') - why does it not work with the jquery val()?如果我使用 this.getAttribute('value') 它会起作用 - 为什么它不适用于 jquery val()?
use采用
alert(jQuery(this).val());
Any of $(this).val()
or $(this).attr("value")
or this.getAttriute("value")
or this.value
will work $(this).val()
或$(this).attr("value")
或this.getAttriute("value")
或this.value
中的任何一个都可以
Please be consistent on $ () and jQuery()请在 $() 和 jQuery() 上保持一致
<script type="text/javascript"><!--
$(document).ready(function(){
$(".cbno").click(function(e){
e.preventDefault();
window.location = $(this).val();
});
});
//-->
</script>
<input type="checkbox" class="cbno" name="content" value="/map/?filter=all" />
because this is not a jQuery object. you need to use $(this).val();因为这不是 jQuery object。你需要使用 $(this).val();
Change:改变:
alert(this.val());
To:到:
alert($(this).val());
$(this)
refers to checkbox you are after. $(this)
指的是您之后的复选框。
You can also simply use:您也可以简单地使用:
this.value
So you can modify your code like this:所以你可以这样修改你的代码:
$(document).ready(function(){
jQuery(".cbno").click(function(e){
e.preventDefault();
alert(this.value);
//window.location = this.value;
});
});
$(document).ready(function(){
jQuery(".cbno").click(function(e){
e.preventDefault();
alert('test');
alert( $(this).val());
// window.location = $(this).val();
});
});
this in events function is a link to the DOM element, so you should create a jQuery object with it`s methods to take them这在事件 function 中是指向 DOM 元素的链接,因此您应该使用它的方法创建一个 jQuery object 来获取它们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.