首页 > 解决方案 > 如何使用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); 

该值包含诸如“主键”之类的空格,但我只有主键如何获得空格的值

标签: javascriptjquery

解决方案


解决方法很简单,你只是忘了给 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 将正确选择值


推荐阅读