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

标签 jquery ajax

Jsfiddle:http://jsfiddle.net/Xytvg

每个复选框都有 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>

最佳答案

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

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

关于Jquery 选择 checkbix 并构建要发送到数据库的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16723582/

相关文章:

javascript - 我无法在我的 html 页面中显示 ajax 请求响应

javascript - jquery事件不会自动触发

jquery - 悬停在 IE9 中无法与 iFrame 配合使用

javascript - Ajax:将内容从另一个div加载到div中

javascript - 无需 jQuery 即可组合两个 AJAX

javascript - 通过单击按钮打开 js 弹出窗口

javascript - 通过 ajaxsetup 发布带有附加参数的表单序列化数据

javascript - 将不透明的图像绘制到 Canvas 上

php - Jquery:向上滑动div(如果最后)

javascript - beforeunload 中的 ajax 会可靠地执行吗?