【问题标题】:ajax response in input value attribute输入值属性中的ajax响应
【发布时间】:2016-09-21 20:08:27
【问题描述】:

评论回复正常工作,但我想做$('#total_products').attr('value') = response; 之类的事情,但这不起作用。为什么这不起作用,我该如何解决?

$(document).ready(function(){

  $.ajax({
    type : 'post',
    url : 'product_store.php',
    data : {
      total_products: "totalproducts"
    },
    success:function(response) {
      $('#total_products').attr('value') = response;
      /*document.getElementById("total_products").value=response;*/
    }
  });

})

【问题讨论】:

  • document.getElementById("total_products").value(response);
  • $('#total_products').attr('value',response)
  • 感谢它的工作原理。
  • $('#total_products').val(response);

标签: jquery ajax jquery-selectors


【解决方案1】:

虽然上面的解决方案是对的,但是在 jQuery 中正常的做法是:


$('#total_products').val(response);

【讨论】:

    【解决方案2】:

    使用这个:

    $('#total_products').attr('value',response);
    

    jQuery attr 文档:

    http://api.jquery.com/attr/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      相关资源
      最近更新 更多