请问js的一个小疑点
请教js的一个小问题。
html代码
问题是我这样获取不到所需要的值,js部分应该怎么写?
------解决方案--------------------
alert(obj.acontent.value);
你应该这么写 才能获取到值
------解决方案--------------------
------解决方案--------------------
alert(obj.content.value);
content 这个元素名用变量代替行吗?在这里这种情况可能js会把content直接当做元素名去查找了吧!你这样试试呢:
alert(obj.(+content+).value);
若不行的话你还是就用if去判读吧
------解决方案--------------------
为什么不加个id使用
document.getElementById(content).value
使用jquery更简单
$('#'+content).val()
------解决方案--------------------
<script type="text/javascript">
function CheckInput(obj,type){
var content = type+'content';
alert(content);
alert(obj.firstChild.value);
//问题出在这里,怎么获取不到表单的值呢?
return false;
}
</script>
这么写
------解决方案--------------------
html代码
- HTML code
<script type="text/javascript"> function CheckInput(obj,type){ var content = type+'content'; alert(content); alert(obj.content.value); //问题出在这里,怎么获取不到表单的值呢? return false; } </script> <form action="save.php" method="post" name="aform" onsubmit="return CheckInput(this,'a')"> <input type="text" name="acontent" /> <input type="submit" name="submit" value="发布" /> </form> <form action="save.php" method="post" name="bform_1" onsubmit="return CheckInput(this,'b')"> <input type="text" name="bcontent" /> <input type="submit" name="submit" value="发布" /> </form> <form action="save.php" method="post" name="bform_2" onsubmit="return CheckInput(this,'b')"> <input type="text" name="bcontent" /> <input type="submit" name="submit" value="发布" /> </form>
问题是我这样获取不到所需要的值,js部分应该怎么写?
------解决方案--------------------
alert(obj.acontent.value);
你应该这么写 才能获取到值
------解决方案--------------------
------解决方案--------------------
alert(obj.content.value);
content 这个元素名用变量代替行吗?在这里这种情况可能js会把content直接当做元素名去查找了吧!你这样试试呢:
alert(obj.(+content+).value);
若不行的话你还是就用if去判读吧
------解决方案--------------------
为什么不加个id使用
document.getElementById(content).value
使用jquery更简单
$('#'+content).val()
------解决方案--------------------
<script type="text/javascript">
function CheckInput(obj,type){
var content = type+'content';
alert(content);
alert(obj.firstChild.value);
//问题出在这里,怎么获取不到表单的值呢?
return false;
}
</script>
这么写
------解决方案--------------------
- JScript code
<script type="text/javascript"> function CheckInput(obj,type){ var content = type+'content'; alert(obj.content.value); //对象没有这么组合的。应该改为:obj.elements[content].value; return false; } </script>