jQuery 无法获取输入值
jQuery cannot get input value
我试图在 jQuery 按键后获取输入值。我尝试了几种方法来获取它,但我收到了 undefined
或 ""
。使用 JSF+RichFaces。输入是 <h:inputTextarea>
尝试通过以下方式获取值:
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#myform:idTextarea").keyup(function(){
var testValue = jQuery("myform:idTextarea").val();
console.log(testValue);
});
});
</script>
日志中的变量是undefined
。
当我像那样添加 "input:" 时
var testValue = jQuery("input:myform:idTextarea").val();
我得到 j_id12
.
当这样改变时:
var testValue = jQuery("#myform:idTextarea").val();
我得到 ""
.
有人有线索吗?
您需要在构建 jquery 选择器时转义 CSS 元字符:
jQuery("input\:myform\:idTextarea").val();
您不需要找到注册了 keyup 的相同元素,只需使用 this
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#myform:idTextarea").keyup(function(){
var testValue = jQuery(this).val();
console.log(testValue);
});
});
</script>
我试图在 jQuery 按键后获取输入值。我尝试了几种方法来获取它,但我收到了 undefined
或 ""
。使用 JSF+RichFaces。输入是 <h:inputTextarea>
尝试通过以下方式获取值:
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#myform:idTextarea").keyup(function(){
var testValue = jQuery("myform:idTextarea").val();
console.log(testValue);
});
});
</script>
日志中的变量是undefined
。
当我像那样添加 "input:" 时
var testValue = jQuery("input:myform:idTextarea").val();
我得到 j_id12
.
当这样改变时:
var testValue = jQuery("#myform:idTextarea").val();
我得到 ""
.
有人有线索吗?
您需要在构建 jquery 选择器时转义 CSS 元字符:
jQuery("input\:myform\:idTextarea").val();
您不需要找到注册了 keyup 的相同元素,只需使用 this
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#myform:idTextarea").keyup(function(){
var testValue = jQuery(this).val();
console.log(testValue);
});
});
</script>