如下图,想用js判断值,但是怎么就是取不到value呢?
下面是表格和和表单及js代码。
<form name="form" id="form" method="post" action="" style="margin:0px; padding:0px;">
<table name="table" id="table" width="100%" border="1" style="border-collapse:collapse;border:1;font-size:12px;height:auto">
<th width="18%"></th><th width="25%"></th><th width="18%"></th><th width="38%"></th>
<tr>
<td colspan="3" ><input name="target" id="targetid" type="text" class="tb_input" placeholder="客户简称+项目简称"></td>
</tr>
</table><script>
var abc=document.getElementById("target");
var b=document.getElementById("targetid");
alert("提交失败!this is a value:"+abc.value);
</script>取值用好多方法:
var abc=form.elements[0].value
var abc=form.target.value
var abc=document.forms[1].elements[0].value
等等,都不行,真是奇怪了
我是先建了个form,再把table放在form里,这样表格比较直观清晰些,但是js就是取不到值,郁闷!
下面是表格和和表单及js代码。
<form name="form" id="form" method="post" action="" style="margin:0px; padding:0px;">
<table name="table" id="table" width="100%" border="1" style="border-collapse:collapse;border:1;font-size:12px;height:auto">
<th width="18%"></th><th width="25%"></th><th width="18%"></th><th width="38%"></th>
<tr>
<td colspan="3" ><input name="target" id="targetid" type="text" class="tb_input" placeholder="客户简称+项目简称"></td>
</tr>
</table><script>
var abc=document.getElementById("target");
var b=document.getElementById("targetid");
alert("提交失败!this is a value:"+abc.value);
</script>取值用好多方法:
var abc=form.elements[0].value
var abc=form.target.value
var abc=document.forms[1].elements[0].value
等等,都不行,真是奇怪了
我是先建了个form,再把table放在form里,这样表格比较直观清晰些,但是js就是取不到值,郁闷!
<form name="form" id="form" method="post" action="" style="margin:0px; padding:0px;">
<table name="table" id="table" width="100%" border="1" style="border-collapse:collapse;border:1;font-size:12px;height:auto">
<th width="18%"></th><th width="25%"></th><th width="18%"></th><th width="38%"></th>
<tr>
<td colspan="3" ><input name="target" id="targetid" type="text" value="明明有值" class="tb_input" placeholder="客户简称+项目简称"></td>
</tr>
</table><script>
alert(document.getElementById("targetid").value);
</script>
var b=document.getElementById("targetid");
alert("提交失败!this is a value:"+abc.value);//你却输abc
alert(hehe);
2.没有名为target的id,
var abc=document.getElementsByName("target")[0];
var abc=document.getElementById("target");
var b=document.getElementById("targetid");
alert("提交失败!this is a value:"+abc.value);你alert是要取abc的value值,但是你abc是id为targetid的元素,但是你的html中没有。我看你的意思应该是取这个input的值,那么你alert中就不应该是abc.value而是b.value。
2.你的alert是在html渲染完成后就触发,此时你的input没有值,所以你取到的value值为空,如果你想有值的话,就给input增加属性value="00"再试试。或者更改js