我有一个包含 2 个 block 的函数,第一个 block 在某些页面元素中添加 html,第二个 block 填充表单上的输入字段。我无法让他们工作。我的意思是,一次只有一个工作。
function popReadMsg(div_id, msg_id, msg, sender, receiver, sent, msg_vid_id)
{
$('#msg_sender').html(sender);
$('#msg_sent').html(sent);
$('#msg_body').html(msg)
$('#vid_id').html(vid_id);
// this part does not work unless I remove the top block.
$('#reply_recipient').prop('value', sender);
$('#reply_sender').prop('value', receiver);
$('#reply_vid_id').prop('value', vid_id);
}
我尝试将第二个 block 包装在函数内并调用它:
function popReadMsg(div_id, msg_id, msg, sender, receiver, sent, msg_vid_id)
{
$('#msg_sender').html(sender);
$('#msg_sent').html(sent);
$('#msg_body').html(msg)
$('#vid_id').html(vid_id);
populateFields(sender, receiver, msg_vid_id);
}
function populateFields(sender, receiver, msg_vid_id)
{
$('#reply_recipient').prop('value', sender);
$('#reply_sender').prop('value', receiver);
$('#reply_vid_id').prop('value', vid_id);
}
HTML:
<div class="read_more_inner_wrapper">
<p class="output_msg2"></p>
<table border="0">
<tr>
<td><p id="msg_sender"></p></td>
<td><p id="msg_sent"></p></td>
</tr>
<tr>
<td><p id="msg_body"></p></td>
</tr>
</table>
</div>
<div class="hide reply_form" >
<form class="msg_form" onSubmit="return false">
<input type="text" name="reply_sender" id="reply_sender" >
<input type="text" name="reply_recipient" id="reply_recipient" >
<input type="text" name="reply_vid_id" id="reply_vid_id">
<textarea id="reply_msg" name="reply_msg" cols="10" rows="10" placeholder="Write your reply here"></textarea>
<br>
<input type="button" onClick="replyMsg();" value="Send">
</form>
</div>
没有成功:(
欢迎任何帮助, 迈克
最佳答案
我认为问题出在这一行:
$('#vid_id').html(vid_id);
据我所知,“vid_id”未定义(有一个名为“div_id”的参数,可能是拼写错误?),因此该行将抛出“div_id is undefined”错误,并且该函数将停止执行。当您删除第一 block 代码时,这种情况就不会发生。
您应该学习使用控制台,每个现代浏览器的开发人员工具中都有它。它会帮助您立即找到此错误。
关于javascript - 另一个函数内的函数调用未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22136140/