Jquery/ajax/php单击复选框后将数据加载到文本字段中

Jquery/ajax/php单击复选框后将数据加载到文本字段中,php,jquery,ajax,checkbox,Php,Jquery,Ajax,Checkbox,如何在单击复选框后将数据加载到文本字段中&执行此操作后,我需要禁用该文本框,但再次取消选中时,需要删除该数据并启用文本框以手动输入数据。我试过这段代码,我是jquery/ajax新手,不知道如何解决这个问题 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(

如何在单击复选框后将数据加载到文本字段中&执行此操作后,我需要禁用该文本框,但再次取消选中时,需要删除该数据并启用文本框以手动输入数据。我试过这段代码,我是jquery/ajax新手,不知道如何解决这个问题

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>

 <script>
$(document).ready(function(){
   $('#chk').click(function(){
     if($(this).is(":checked"))
       $("#txtaddress").removeAttr("disabled");
    else
      $("#txtaddress").attr("disabled" , "disabled");
         // here, i don't know how do i assign $row['address'] to txtaddress by
         // using php mysql 
    });
   });
 </script>

      <!-this is my html code-->
      <form>
        <input type="checkbox" id="chk" name="chk">Same as home address?
        <input id="txtaddress" name="txtaddress"  disabled type="text">
       </form>

$(文档).ready(函数(){
$('#chk')。单击(函数(){
如果($(this).is(“:checked”))
$(“#txtaddress”).removeAttr(“禁用”);
其他的
$(“#txtaddress”).attr(“禁用”、“禁用”);
//在这里,我不知道如何通过
//使用php-mysql
});
});
和家庭住址一样吗?
javascript代码:

$(document).ready(function(){
 $('#chk').click(function(){
var txt =   $("#txtaddress");
 ($(this).is(":checked")) ? txt.attr("disabled" , true) :  txt.attr("disabled" , false);
 });
});
试试下面的方法

<script>
$(document).ready(function(){
        $('#chk').click(function(){
            if($(this).is(":checked"))
                 $("#txtaddress").val(""); //reset the textbox
                $("#txtaddress").removeAttr("disabled");

            else {
                $.ajax({
                    type: 'GET',
                    url: destinationUrl, // your url
                    success: function (data) { // your data ie $row['address'] from your php script
                        $("#txtaddress").val(data); //set the value
                        $("#txtaddress").attr("disabled", "disabled"); 
                    }
                });
            }
        });
});
</script>

$(文档).ready(函数(){
$('#chk')。单击(函数(){
如果($(this).is(“:checked”))
$(“#txtaddress”).val(“”;//重置文本框
$(“#txtaddress”).removeAttr(“禁用”);
否则{
$.ajax({
键入:“GET”,
url:destinationUrl,//您的url
成功:函数(数据){//php脚本中的数据即$row['address']
$(“#txtaddress”).val(数据);//设置值
$(“#txtaddress”).attr(“禁用”、“禁用”);
}
});
}
});
});

从您的
php
脚本中,您可以
echo
$row['address']]
,这样成功函数中的数据就可以放入
ajax

的文本框中,我希望这就是您的意思。。希望这能有所帮助……:)


从php文件
echo
中选择所需的
$row['address']
。此
值将在
ajax函数的
success
中获得。对于初学者,
。atte
应该是
.attr
。然后在下一行dodo
$(“#txtaddress”).val(“”)
我需要禁用该复选框,但当我取消选中它时,
-你如何单击“禁用复选框”使其再次取消选中?这是不可能的,我想他是指在选中复选框时禁用文本输入,至少我想是这样。@JamieTaylor:对不起,这是文本框,我编辑的。
    $('#chk').click(function(){
           if(! $(this).is(":checked")){
               $("#txtaddress").removeAttr("disabled");
                $("#txtaddress").val('');
           }
           else{
               $.ajax( {
                url: "get_row_details",
                type: "POST", //or may be "GET" as you wish
                data: 'username='+name, // Incase u want to send any data
               success: function(data) {
                   $("#txtaddress").val(data); //Here the data will be present in the input field that is obtained from the php file
               }
               });
               $("#txtaddress").attr("disabled" , "disabled");
           }
        });