【问题标题】:how to set radio checked of an input by .val()?如何通过.val()设置无线电检查输入?
【发布时间】:2013-07-04 01:15:40
【问题描述】:

我想按他的值选择一个输入。 问题是我有 4 个无线电输入 当用户单击“下一步”时,它将该答案的值保存在 AnsW 数组中。 当用户单击“返回”时,我希望检查带有我在 AnsW 数组中的值的输入单选按钮,这样用户就可以知道他选择了什么并将他的答案更改为其他内容。

html代码:

<ul>
            <li class="li0">
                <input type="radio" value="ch1" name="choice" id="li0"/>
                <label for="li0"></label>
            </li>
            <li class="li1">
                <input type="radio" value="ch2" name="choice" id="li1"/>
                <label for="li1"></label>
            </li>
            <li class="li2">
                <input type="radio" value="ch3" name="choice" id="li2"/>
                <label for="li2"></label>
            </li>
            <li class="li3">
                <input type="radio" value="ch4" name="choice" id="li3"/>
                <label for="li3"></label>
            </li>
        </ul>

我的代码是:

function go_back(){
    $("#back").bind("click",function(){
        qNum--;
        showQuestion(qNum);
        if(AnsW.length === 0){
            calcAnswers--;
        }
        alert(AnsW[qNum]);
        tempAns = AnsW[qNum];//user last answer which is false i need this to make the radio button point to that answer
        //alert( $("input[value='tempAns']"));
        $("input").val(tempAns).attr('checked', true);
        alert(tempAns);
        //alert(tempAns);
        AnsW.splice(-1,1);//delete the false answer from the array i need this to make sure that the answer is deleted and there wont be overload of wrong answers
        //alert(deleteAns);
        if(qNum === 0){
            $("#back").css({"visibility":"hidden"})
        }
    });
}

【问题讨论】:

  • 你有这些问题吗?还是这些问题是动态的?

标签: javascript jquery html arrays input


【解决方案1】:

jQuery selectors 允许您根据元素的属性查找元素,完全匹配示例:

 $("element.class[attribute=value]")

检查选择器中的value属性

$("input[value="+tempAns+"]").prop('checked', true)

【讨论】:

  • 优点@hungerpain 属性是布尔值,随着元素的当前状态而变化,而属性保持不变。
猜你喜欢
  • 2021-09-23
  • 2013-08-16
  • 2010-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多