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

时间:2013-10-03 11:25:10

标签: 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(){
   $('#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>

3 个答案:

答案 0 :(得分:1)

尝试以下方法,

<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>

您可以在php脚本中echo $row['address'],以便将成功函数中的数据放入ajax

的文本框中

答案 1 :(得分:0)

javascript代码:

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

答案 2 :(得分:0)

我希望这就是你的意思..希望这可能会有所帮助...... :)

    $('#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");
           }
        });

从php文件echo获取所需的$row['address']。此value将在success

ajax function获得