Jquery 在文档准备就绪和更改时获取选择值
我试图在页面加载和更改时获取下拉选择元素(表单中)的值,并将其传递给另一个输入(隐藏)。以下是我迄今为止尝试过的代码 此选项在页面加载时有效,但现在在更改时有效:Jquery 在文档准备就绪和更改时获取选择值,jquery,Jquery,我试图在页面加载和更改时获取下拉选择元素(表单中)的值,并将其传递给另一个输入(隐藏)。以下是我迄今为止尝试过的代码 此选项在页面加载时有效,但现在在更改时有效: jQuery( document ).ready(function() { var comment_post_ID = jQuery("#alldentists").val(); jQuery("#comment_post_ID").val( comment_post_ID );
jQuery( document ).ready(function() {
var comment_post_ID = jQuery("#alldentists").val();
jQuery("#comment_post_ID").val( comment_post_ID );
});
这不适用于pageload,但适用于更改选择选项
jQuery( document ).ready(function() {
jQuery( "#alldentists" ).change(function() {
var comment_post_ID = jQuery("#alldentists").val();
});
jQuery("#comment_post_ID").val( comment_post_ID );
});
有什么方法可以在页面加载和更改时都获得值?我正在全局使用jQuery 1.11.0。声明变量“comment\u post\u ID”
<select name="alldentists" id="alldentists"><option>One</option>
<option>Two</option>
<option>Three</option>
</select>
<input type="text" id="comment_post_ID"/>
jQuery(document).ready(function() {
var comment_post_ID=jQuery("#alldentists").val();
jQuery("#alldentists").on('change', function(e) {
comment_post_ID = jQuery("#alldentists").val();
jQuery("#comment_post_ID").val(comment_post_ID);
});
jQuery("#comment_post_ID").val(comment_post_ID);
});
1
两个
三
jQuery(文档).ready(函数(){
var comment_post_ID=jQuery(#alldentists”).val();
jQuery(#alldentists”).on('change',函数(e){
comment_post_ID=jQuery(“#alldentists”).val();
jQuery(“#comment_post_ID”).val(comment_post_ID);
});
jQuery(“#comment_post_ID”).val(comment_post_ID);
});
演示:
我猜这个
var comment\u post\u ID
限制了变量的范围,使得它只能在.change()
函数中使用。尝试用空值在外部声明它。谢谢,伙计,这正是我需要的。你是救命恩人。