jQuery:选择具有给定名称和值的输入 [英] jquery: select input with given name and value

查看:84
本文介绍了jQuery:选择具有给定名称和值的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查命名为平日且值为1的输入.我尝试了下面的行.它会检查所有工作日.

I want to check the input that is named weekday and has value = 1. I tried the line below. It checks all weekdays.

$('input[name = weekday], [value =1]').attr("checked", "checked");

推荐答案

请勿使用逗号将两个条件应用于同一元素.

Do not use comma to apply both conditions on same element.

$('input[name=weekday][value=1]').attr("checked", "checked");

请注意,对于,您应该使用 prop()代替attr()属性,如 jQuery文档所述,并由@tyleha指出.

As a side note you should use prop() instead of attr() for properties as suggested by jQuery doc and pointed by @tyleha.

从jQuery 1.6开始,.attr()方法返回的属性未定义 尚未设置.检索和更改DOM属性,例如 表单元素的选中,选中或禁用状态,请使用 .prop()方法.

As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method.

您可以使用 .prop(propertyName,value)来设置检查属性,如下所示.

You can use .prop( propertyName, value ) to set the checked property as shown below.

$('input[name=weekday][value=1]').prop("checked", true);

这篇关于jQuery:选择具有给定名称和值的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆