前台页面——js/jq 取/设值

HTML:
1 <body class="section bg bd ra sh mglr">
2       <div class="section pdlr">
3         <input type="text" value="1" id="pagecount_h" >
4         <input type="text" value="1" id="pagenum_h" >
5         </div>
6 </body>
View Code

JS/JQ:

取id="pagecount_h" 的值  --》  $("#pagecount_h").val();

                $("#pagecount_h").attr("value");

取id="pagecount_h" 的值  --》  document.getElementById("pagecount_h").value;

                 

设置id="pagecount_h"的值--》  $("#pagecount_h").val(4);  //这个可以用

               // $("#pagecount_h").attr("value",4); -----------这个方式不好用

设置id="pagecount_h"的值--》 document.getElementById("pagecount_h").value="4";

创建元素并设置熟悉和值--》   var outinput = document.createElement("input");//创建元素

                outinput.setAttribute("value", 4);//为元素设置属性和该属性对应的值

将一个元素添加到另一个元素中--》var outputdiv = document.getElementById("output_Values");获取元素

                outputdiv.appendChild(outinput);//将outinput元素放入outputdiv元素中

            

通过拼接生成html--》

    var trs ="";

    trs += "<td><input data-id=""+value.dimId+"" name="dimensionnm" type="text" id=""+value.dimId+"" value="" onclick="getdims(this)"></td>";    

    $("#partList").html(trs);           

原文地址:https://www.cnblogs.com/BruceDu/p/6765140.html