【问题标题】:perform ajax on-click function if the td is not null如果 td 不为空,则执行 ajax on-click 功能
【发布时间】:2023-03-14 23:38:01
【问题描述】:

目前我有一个在 td 内有图像的表,当点击 td 图像时,我正在执行一些 ajax 函数来更新数据库中的值,我的问题是 td 是否为空(即:td 中没有图像)它也执行相同的功能。如果 td 为空(即:td 中没有图像)我想要什么这样做,谢谢

HTML:

<td style="width:120px" id="CPH_GridView1_Status'.$rows['net_id'].'" class="edit2 status '.$rows["net_id"].' "><img  src="image/'.$rows["status"].'f.png" /></td>

ajax:

<script>
$(document).ready(function(){
  var onClick, ajaxSuccessHandleMaker;
  onClick = function() {
    var
      url = 'clientnetworkpricelist/updateprice.php',
      clientid = $('#client')[0].value,
      classesArray = $(this).attr('class').split(" "),
      // send data as object, jQuery will transparently transform for the server
      data = {
        value : $('.ajax input').val,
        rowid : classesArray[2],
        field : classesArray[1],
        clientid : clientid
      };
    // send POST request and expect JSON
    $.post(url,data,ajaxSuccessHandleMaker(classesArray),'json');
  };
  // success returns the ajax handler with a closure on classesArray
  ajaxSuccessHandleMaker = function (arr) {
    // the handler EXPECTS an array, which is why we have to protect output in updateprice.php
    return function (arrayOf2vals) {
      $('#CPH_GridView1_clientprice'+arr[2]).html(arrayOf2vals[0]);
      $('#CPH_GridView1_Status'+arr[2]).html(arrayOf2vals[1]);
      // I am not sure what you want with the following 2 lines of code
      $('.ajax').html($(this).val());// what is this?
      $('.ajax').removeClass('ajax');// what is this?
    };
  };
  // set the onClick handler
  $('.edit2').click(onClick);
});
</script>

【问题讨论】:

    标签: php jquery ajax dom onclick


    【解决方案1】:

    测试是否有你可以做的图像:

    // your modified function
    onClick = function() {
      var tdElement = $(this);
      if (tdElement.find('img').length > 0) {
        // put your ajax request here....
      }
    }
    

    或者您可以从图像本身而不是 td 触发 ajax 请求。

    【讨论】:

      【解决方案2】:
      $('.edit2').click(function(){
      
            var myvar = this.innerHTML;
      
            if(myvar.length>0) {
              //your code
            }
      
      });
      

      通过edit2类检索数据...如果是您的问题..使用它。

      【讨论】:

        【解决方案3】:
        var _html = $('#CPH_GridView1_Status img').attr('src');
        
         if (_html){
              alert('Image exit');
           // Do your logic
          }
          else{
           alert('image not exit');
          }
        

         var _html = $('#CPH_GridView1_Status img').length ;
        
            if (_html > 0 ){
                  alert('Image exit');
               // Do your logic
              }
              else{
               alert('image not exit');
              }
        

        【讨论】:

          猜你喜欢
          • 2013-11-14
          • 1970-01-01
          • 1970-01-01
          • 2014-01-21
          • 1970-01-01
          • 1970-01-01
          • 2020-10-21
          • 2021-12-01
          • 1970-01-01
          相关资源
          最近更新 更多