4

チェックボックスをクリックした後、テキストフィールドにデータをロードするにはどうすればよいですか?その後、そのテキストボックスを無効にする必要がありますが、チェックを外すと、そのデータを削除し、テキストボックスにデータを手動で入力できるようにする必要があります。私はこのコードを試しました.私は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>
4

3 に答える 3

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

于 2013-10-03T11:49:52.427 に答える
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'].Thisvalueが取得されsuccessますajax function

于 2013-10-03T12:02:36.410 に答える
0

JavaScript コード:

$(document).ready(function(){
 $('#chk').click(function(){
var txt =   $("#txtaddress");
 ($(this).is(":checked")) ? txt.attr("disabled" , true) :  txt.attr("disabled" , false);
 });
});
于 2013-10-03T11:45:38.297 に答える