Jquery选择checkbix并构建要发送到数据库的url

Jquery选择checkbix并构建要发送到数据库的url,jquery,ajax,Jquery,Ajax,Jsfiddle: 每个复选框都有id和值,例如,如果我选中了合格复选框或不合格复选框 我希望禁用该复选框取决于单击了哪个复选框,我希望在成功回调函数时执行此操作 下面是我的代码: $( "input[type=checkbox]" ).on( "click",function( e ){ var preview = $("#preview").html(''); // ** just a loader a gif image preview .html('<i

Jsfiddle:

每个复选框都有id和值,例如,如果我选中了合格复选框或不合格复选框

我希望禁用该复选框取决于单击了哪个复选框,我希望在成功回调函数时执行此操作

下面是我的代码:

$( "input[type=checkbox]" ).on( "click",function( e ){

    var preview = $("#preview").html('');

    // ** just a loader a gif image
    preview .html('<img src="../images/loader.gif" alt="Uploading...."/>');

    // ** this is the value of the checkbox
    var input = $(this).val();

    // ** here i am pulling the id value
    var id    = $(this).attr('id'); 


   $.ajax({
       // here i am trying to build url to be send to the action.php
       // i will get the id and the value depends on which checkbox was clicked 
       // action.php?id=1&value=2

       url: 'action.php?id='+id+'&value='+input, 
       type: "POST",
       success: function( response )
      {
           // here i want to figure out which checkbox was clicked and disabled it
      }
  });

  e.preventDefault();

});



<div id="preview"></div><!--loader gif-->

<p>
   USA
   <input type="checkbox" id="1" value="2" />Qualified OR 
   <input type="checkbox" id="1" value="3" /> Unqualifed
</p>

<p>
   UK 
   <input type="checkbox" id="2" value="2" />Qualified OR 
   <input type="checkbox" id="2" value="3" /> Unqualifed
</p>

<p>
   EGY 
   <input type="checkbox" id="3" value="2" />Qualified OR 
   <input type="checkbox" id="3" value="3" /> Unqualifed
</p>
$(“输入[type=checkbox]”)。在(“单击”上,函数(e){
var preview=$(“#preview”).html(“”);
//**只需加载一个gif图像
html(“”);
//**这是复选框的值
var输入=$(this.val();
//**我在这里提取id值
var id=$(this.attr('id');
$.ajax({
//在这里,我试图构建要发送到action.php的url
//我将获得id,其值取决于单击的复选框
//action.php?id=1&value=2
url:'action.php?id='+id+'&value='+input,
类型:“POST”,
成功:功能(响应)
{
//在这里,我想找出哪个复选框被点击并被禁用
}
});
e、 预防默认值();
});

美国
合格的或
不合格

英国 合格的或 不合格

埃吉 合格的或 不合格


将复选框分配给单击处理程序中的变量,如下所示:

$( "input[type=checkbox]" ).on( "click",function( e ) {
    var $checkbox = $(this);
    // rest of click handler code
$.ajax({
   url: 'action.php?id='+id+'&value='+input, 
   type: "POST",
   success: function( response )
   {
       $checkbox.attr('disabled', true);
   }
});
然后在成功回调中,您可以按如下方式禁用它:

$( "input[type=checkbox]" ).on( "click",function( e ) {
    var $checkbox = $(this);
    // rest of click handler code
$.ajax({
   url: 'action.php?id='+id+'&value='+input, 
   type: "POST",
   success: function( response )
   {
       $checkbox.attr('disabled', true);
   }
});
给你:

$(“输入[type=checkbox]”)。在(“单击”上,函数(e){
//您只需要在此处本地化var:)
var$this=$(this);
var preview=$(“#preview”).html(“”);
html(“”);
var输入=$(this.val();
var id=$(this.attr('id');
$.ajax({
url:'action.php?id='+id+'&value='+input,
类型:“POST”,
成功:功能(响应){
//我们在上面添加了本地化的var,因此现在可以访问此范围:)
$this.attr(“禁用”、“禁用”);
}
});
e、 预防默认值();
});

感谢clav的重播,但是在成功回调函数中,在单击处理程序中指定
$checkbox
变量,然后在成功回调中禁用该复选框。我更新了答案。谢谢,老兄,上帝保佑你,兄弟,我明白了。你看,我只是在学习jQuery,我只学了一个星期,所以我没有快速解决问题的经验。谢谢你,我真的很感激。有没有更好的方法来构建url,而不是像上面这个那样,而不是像这个丑陋的“action.php”呢?id='+id+'&value='+input,我刚想出来,但我不知道如何让事情变得更好