我试图将一个变量(带有撇号的名字)从 HTML 传递到 jQuery。当值到达 jQuery 时,字符串丢失了撇号后面的所有字符。
名称示例:Test'Ing
这是表格的一部分。我动态填充列表,并且从数据库中提取名称时显示正确:
<?php echo "<select id='hiringManager' class='dropdown_form' name='hiringManager'>";
echo "<option disabled selected>Hiring Manager</option>";
while ($result_row = mysqli_fetch_row($hiring_manager_query)) {
echo "<option value='" . $result_row[0] . "'>" . $result_row[0] . "</option>";
}
echo "</select>" ?>
然后我从表单中获取值并将其显示在 jQuery 模式中以进行确认:
$('#hiringManagerModal').text($('#hiringManager').val());
显示的结果文本是:
“测试”而不是“测试'Ing”
我四处寻找,但未能找到解决此问题的帖子,或者我无法在搜索中正确表述该问题。
感谢任何帮助。谢谢!
最佳答案
问题出在 PHP 代码上。我已将 '
替换为 "
,并将 "
替换为 '
。之前输出的 HTML 是,
<select id='hiringManager' class='dropdown_form' name='hiringManager'>
<option disabled selected>Hiring Manager</option>
<option value='test'ing'>test'ing</option> <!-- In this option, the value is test instead of test'ing -->
</select>
现在在所有替换之后,它是,
<select id="hiringManager" class="dropdown_form" name="hiringManager">
<option disabled selected>Hiring Manager</option>
<option value="test'ing">test'ing</option><!-- Now, here the value is test'ing, as required -->
</select>
真正发生的是,当遇到 '
时,字符串被终止,结果只输出“test”而不是“test'ing”。
所有替换后更正后的 PHP 代码:
<?php
echo '<select id="hiringManager" class="dropdown_form" onchange="displayInModal();" name="hiringManager">';
echo '<option disabled selected>Hiring Manager</option>';
while ($result_row = mysqli_fetch_row($hiring_manager_query)) {
echo '<option value="' . $result_row[0] . '">' . $result_row[0] . '</option>';
}
echo '</select>'
?>
与,
$('#hiringManagerModal').text($('#hiringManager').val());
现在,同样的问题也会出现
<option value="test"ing"></option>
其他方式:
echo '<option value='test\'ing'></option>';
关于javascript - 将字符串从 HTML 发送到 jQuery 时,jQuery 删除撇号后的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41497104/