JavaScript:使用 jQuery 获取值并替换它
我有一个性别选择器和一个脚本,该脚本获取所选性别的值并将其粘贴到标签上,但它粘贴的值是:“M”或“F”。
<div name="userLabelDiv" id="genderUserLabelDiv">
<label class="required">Sexo</label>
<label id="genderLabel">F</label>
</div>
现在,当我尝试获取这封信并替换为实际性别时,我缺少 JS 脚本。你能帮我吗?
$("#genderLabel").html(
if ($(this).value == 'F') {
this.value = 'Female';
}
if ($(this).value == 'M') {
this.value = 'Male';
}
);
我知道脚本可能有误,请问有人可以纠正吗?谢谢你!
I have a gender selector, and a script that gets the value of the selected gender and pastes it on a label, but it's pasting the value: "M" or "F".
<div name="userLabelDiv" id="genderUserLabelDiv">
<label class="required">Sexo</label>
<label id="genderLabel">F</label>
</div>
Now when I'm trying to get this letter and replace for the actual gender, I'm missing on the JS script. Could you help me?
$("#genderLabel").html(
if ($(this).value == 'F') {
this.value = 'Female';
}
if ($(this).value == 'M') {
this.value = 'Male';
}
);
I know the script is probably wrong, could someone correct it, please? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 jQuery 1.4+ 中,您可以将函数传递给
.text()
像这样:这会将
F
转换为Female
,将M
转换为Male
并保留其他任何内容,以防出现新的性别过来 :)In jQuery 1.4+ you can pass a function to
.text()
like this:This converts
F
toFemale
,M
toMale
and leaves anything else alone, in case new genders come up :)将每个
value
替换为text()
以及其他一些内容
replace every
value
withtext()
Among a few other things
要以这种方式使用
.html()
,您需要传递一个function
作为返回所需值的参数。尝试一下: http://jsfiddle.net/FDZBJ/
To use
.html()
that way, you need to pass afunction
as the argument that returns the value you want.Try it out: http://jsfiddle.net/FDZBJ/