javascript - 如何使用jQuery获取输入类型radio的值,该值包含空格
问题描述
我使用了这段代码 我也在我的页面中使用了 AJAX
$html.='<div><p id="question" name="qid">Q.'.$result['question_no'].': '.$result['question'].'</p></div>
<label style="padding-top:15px;">
A <span style="padding-left: 15px;"><input type="radio"';
if (isset($result['answer']) && $result['answer']==$result['choice1']) {
$html.= 'checked="checked"';
}
$html.=' name="answer" id="option" value='.$result['choice1'].'>'.$result['choice1'].'</span>
</label>
</div>
jQuery部分是
var answer = $("form input[type='radio']:checked").val();
alert(answer);
该值包含诸如“主键”之类的空格,但我只有主键如何获得空格的值
解决方案
解决方法很简单,你只是忘了给 value 属性加上双引号, value 属性必须用双引号括起来这里是更正的代码,
$html.='<div><p id="question" name="qid">Q.'.$result['question_no'].': '.$result['question'].'</p></div>
<label style="padding-top:15px;">
A <span style="padding-left: 15px;"><input type="radio"';
if (isset($result['answer']) && $result['answer']==$result['choice1']) {
$html.= 'checked="checked"';
}
$html.=' name="answer" id="option" value="'.$result['choice1'].'">'.$result['choice1'].'</span>
</label>
</div>';
现在 jquery 将正确选择值
推荐阅读
- geometry - 有没有办法从 osm 中提取尚未归属的多边形?
- javascript - 我想映射字典然后映射字典键中的值?
- django - help_text - Django Crispy Forms
- javascript - 如何使用反应验证动态输入字段
- javascript - Firebase 上传文件时出错:AppCheck:AppCheck 正在activate() 之前使用
- javascript - 离线事件是否可以检测到 ERR_CONNECTION_RESET?
- reactjs - 如何遍历所有 Axios Api 数据并使用 React Hooks 将其传递给另一个容器
- r - 在 R 中的迷你图中悬停值和类别
- .net - 将所有字符串类型的值从托管堆转储到文件 - WinDbg
- bash - 在 ftp bash 会话中休眠