<script>
var myTable = document.getElementById("myTable2");
if (myTable != null) {
var myRow = myTable.insertRow();
myRow.insertCell().innerHTML = '<input type="text" ID="tPrice" name="textDepotID" style="width:120px;border-width:0px;text-align:center;border-style:none" readonly="readonly" value="' + txtSellprice + '"/>';
}
</script>我要取得该行的该input,id为tPrice的value值,我用jquery的话。这么写
var a=$(this).children("#tPrice").val()
不知道为啥,取不到值。下面为参考代码。
$("#myTable2 tr ").live({
"keydown": function (e) {
var Key = e.keyCode || e.which || e.charCode;
if (Key == 98) {//小键盘-号
// alert(Key);
var k = window.showModalDialog("altershow.htm", window, "dialogWidth:270px;status:no;dialogHeight:176px");
if (k != null)//这里k值我是可以确定返回值没错。
$(this).children("#tPrice").val() = k;//这里似乎获取的inputID错误了。
}
}
})
var myTable = document.getElementById("myTable2");
if (myTable != null) {
var myRow = myTable.insertRow();
myRow.insertCell().innerHTML = '<input type="text" ID="tPrice" name="textDepotID" style="width:120px;border-width:0px;text-align:center;border-style:none" readonly="readonly" value="' + txtSellprice + '"/>';
}
</script>我要取得该行的该input,id为tPrice的value值,我用jquery的话。这么写
var a=$(this).children("#tPrice").val()
不知道为啥,取不到值。下面为参考代码。
$("#myTable2 tr ").live({
"keydown": function (e) {
var Key = e.keyCode || e.which || e.charCode;
if (Key == 98) {//小键盘-号
// alert(Key);
var k = window.showModalDialog("altershow.htm", window, "dialogWidth:270px;status:no;dialogHeight:176px");
if (k != null)//这里k值我是可以确定返回值没错。
$(this).children("#tPrice").val() = k;//这里似乎获取的inputID错误了。
}
}
})
应该是:
var a=$(this).find("#tPrice").val()
分析下你的语句吧,让你更清楚些
var a=$(this)//因为处于tr集合的循环体,所以,这里$(this)得到的是每个<tr>
.children("#tPrice")//你使用了children(),它只获取子元素而不考虑所有后代元素。因为<tr>的子元素是<td>,因此,你这选择器其实是去查找<td id="tPrice">的元素去了。所以你找不着
.val()
1:仔细的看看jquery的选择器
2:碰到这种自己拎不清的。你就打印,用alert()去打印html().以你这个例子来说:alert($(this).children("#tPrice").html()).你得到结果:undefined 就已经表示的选择器定义错了初学时,对选择器不熟悉的话,很多时候可以选择将当前自己的选择器的DOM打印出来,看看是不是就容易明白问题出在哪里了