Javascript 如果没有文本,Jquery将阻止输入框触发器,但不会阻止textarea触发器
我的输入框使用占位符文本和jquery来确保在我添加文本(即非占位符文本)之前不会触发。我想用文本区域替换输入框,但无法使jquery正常工作。对于下面的文本区域,无论我是否在文本区域中键入,提交按钮都会触发Javascript 如果没有文本,Jquery将阻止输入框触发器,但不会阻止textarea触发器,javascript,php,jquery,textarea,inputbox,Javascript,Php,Jquery,Textarea,Inputbox,我的输入框使用占位符文本和jquery来确保在我添加文本(即非占位符文本)之前不会触发。我想用文本区域替换输入框,但无法使jquery正常工作。对于下面的文本区域,无论我是否在文本区域中键入,提交按钮都会触发 <div class="RepForm-loggedin" > <form name="RepForm" method="POST" action="<?php echo $this->newReply;?>"> <table n
<div class="RepForm-loggedin" >
<form name="RepForm" method="POST" action="<?php echo $this->newReply;?>">
<table name="RepFormTable" style="width:100%">
<tr>
<td style="width:100%">
//CURRENT INPUT BOX
<input type="text" rows="8" id="repContent" class="inputbox inputbox-title placeholder-soc-med" name="repContent" placeholder="Ask a question . . ."/>
//DESIRED REPLACEMENT TEXT AREA
<textarea type="text" rows="8" style="max-width:600px; width:100%" id="repContent" class="inputbox inputbox-title placeholder-soc-med" name="repContent" placeholder="Ask a question . . ."></textarea>
</td>
</tr>
<tr>
<td style = "text-align:right;">
<input type="submit" value="Submit" id="sendComment" class="dgrey-button">
</td>
</tr>
</table>
</form>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#sendComment').attr('disabled',true);
$('#repContent').keyup(function(){
if($(this).val().length !=0)
$('#sendComment').attr('disabled', false);
else
$('#sendComment').attr('disabled',true);
})
});
</script>
您有重复的id=repContent
您的输入框也有此ID。因此,当您使用$'repContent'选择元素时,它只返回您的输入字段
只需从中删除此属性,代码就可以工作
工作小提琴:
您可能会问:$'id'为什么不选择具有此id的所有元素
首先,文档中只能有一个ID相同的元素。从:
id=此属性为元素指定名称。此名称在文档中必须是唯一的
然后,当您使用jQuery选择by id时,它将只返回第一个匹配项,因为它在内部使用document.getElementById。从:
每个id值在文档中只能使用一次。如果超过
一个元素被分配了相同的ID,即使用该ID的查询
将仅选择DOM中第一个匹配的元素。这种行为
然而,不应依赖于;包含多个文档的文档
使用相同ID的元素无效