jquery从单选按钮和文本字段中获取数据

时间:2014-01-22 10:47:44

标签: jquery forms text input radio-button

我有100多个问题的调查,其中有无线电按钮或文本字段。

我想出了如何从radiobuttons获得结果,但我无法从textfields获得与radiobuttons相同的结果变量,称为temp。

我使用以下代码从radiobuttons获取结果。

if($questions.find("input:radio:checked").length === $questions.length)
{
    var temp ='';

    $(':radio').each(function()
    {
        if($(this).is(':checked'))
            {
          temp += $(this).val()+ ",";
         }
        });

        var currentSurveyName = getQueryVariable("sn");
        var strResult = temp;

        window.location="survey-result.php?sn=" + currentSurveyName + "&sr=" + strResult;   
        }
        else
        {
            alert("Need to check all radiobuttons");
        }   

我的表单代码是这样生成的:

<div class='aQuestion' id='div1'>
<h3>question 1</h3>
<label for='a108'><input type='radio' id='a108' name='grp108' value='0'><span class='answerOption'>0. Answer</span></br></label>
<label for='b108'><input type='radio' id='b108' name='grp108' value='1'><span class='answerOption'>1. Answer</span></br></label>
<label for='c108'><input type='radio' id='c108' name='grp108' value='2'><span class='answerOption'>2. Answer</span></br></label>
<label for='d108'><input type='radio' id='d108' name='grp108' value='3'><span class='answerOption'>3. Answer</span></br></label>
<label for='e108'><input type='radio' id='e108' name='grp108' value='4'><span class='answerOption'>4. Answer</span></label>
</div>

<div class='aQuestion' id='div2'>
<h3>question 2</h3>
<label for='a110'><input type='radio' id='a110' name='grp110' value='0'><span class='answerOption'>0. Answer</span></br></label>
<label for='b110'><input type='radio' id='b110' name='grp110' value='1'><span class='answerOption'>1. Answer</span></br></label>
<label for='c110'><input type='radio' id='c110' name='grp110' value='2'><span class='answerOption'>2. Answer</span></br></label>
<label for='d110'><input type='radio' id='d110' name='grp110' value='3'><span class='answerOption'>3. Answer</span></br></label>
<label for='e110'><input type='radio' id='e110' name='grp110' value='4'><span class='answerOption'>4. Answer</span></label>
</div>

<div class='aQuestion' id='div3'>
<h3>question 3</h3>
<input type='text' class='inputField' id='a113' name='grp113'>cm
</div>

<div class='aQuestion' id='div4'>
<h3>question 4</h3>
<input type='text' class='inputField' id='a114' name='grp114'>kg
</div>

提前致谢:D告诉我您是否需要更多信息/代码。

如果我想检查文本字段,例如我检查下面的单选按钮,该怎么办?代码怎么样呢?

$('.aQuestion').each(function()
        {
            if($(this).find('input[type="radio"]:checked').length > 0)
            {
                $(this).addClass( "madeChoice" ); 
            }
            else
            {   
                $(this).children("h3").addClass( "didntMakeChoice" );                
            }   

        });  

1 个答案:

答案 0 :(得分:0)

这就是你对收音机所拥有的:

$(':radio').each(function()
{
    if($(this).is(':checked'))
    {
        temp += $(this).val()+ ",";
    }
});

只需将其更改为

即可
$(':text').each(function()
{
    if($(this).val() !== "")
    {
        temp += $(this).val() + ",";
    }
});