【问题标题】:Jquery/ajax/php load data into textfield after clicking checkbox单击复选框后,Jquery/ajax/php 将数据加载到文本字段中
【发布时间】:2013-10-10 01:54:35
【问题描述】:

单击复选框后如何将数据加载到文本字段中,完成后,我需要禁用该文本框,但再次取消选中它时,需要删除该数据并启用文本框以手动输入数据。我试过这段代码,我是 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>

【问题讨论】:

  • 对于初学者,.atte 应该是 .attr。然后在下一行做$('#txtaddress").val('&lt;?php echo $row[address];?&gt;');
  • i need to disable that checkbox, but again as i uncheck it, - 如何单击禁用复选框使其再次取消选中?这是不可能的
  • 我认为他的意思是至少在选中复选框时禁用文本输入。
  • @JamieTaylor:对不起,这是文本框,我编辑了。

标签: php jquery ajax checkbox


【解决方案1】:

javascript代码:

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

【讨论】:

    【解决方案2】:

    试试下面的,

    <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的文本框中

    【讨论】:

      【解决方案3】:

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

          $('#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 将在ajax functionsuccess 处获得

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-05-21
        • 1970-01-01
        • 2012-05-28
        • 1970-01-01
        • 1970-01-01
        • 2012-12-07
        • 2011-09-30
        相关资源
        最近更新 更多