我正在尝试将文本框的值(id = msg)传递给 js,但输出始终是 foreach 循环上的第一个文本框值。请帮我。这是代码。
JS:
function sub() {
var name = document.getElementById('msg').value;
alert(name);
}
HTML:
<?php
foreach($messages->result() as $msg):
foreach($agentname->result() as $agnt):
if($msg->message_status == 1):
$message_status = "<font class='icon-exclamation-sign'></font> <font class='purple'>New message!</font>";
else:
$message_status = "";
endif;
?>
<div class="accordion-wrapper" style="margin-top:0">
<a id="message" onclick="return sub();" rel="msg<?php echo $msg->message_id;?>" id = "button_id" style="background-color:#C2E4CD" href="javascript:void(0)" class="accordion-title blue"><span><?php echo $message_status; ?> <font class="icon-comment"></font> <font class="orange" >From:</font> <?php echo $agnt->agent_shortname;?> | <font class="icon-envelope-alt"></font> <font class="orange">Subject:</font> <?php echo $msg->message_title;?></span></a>
<div class="accordion-content">
<input type="text" id="msg" value="<?php echo $msg->message_id;?>" />
<p><?php echo $msg->message;?></p>
</div>
</div>
<?php endforeach; ?>
<?php endforeach; ?>