JavaScript:使用 jQuery 获取值并替换它

发布于 2024-09-14 02:26:46 字数 527 浏览 4 评论 0原文

我有一个性别选择器和一个脚本,该脚本获取所选性别的值并将其粘贴到标签上,但它粘贴的值是:“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不喜欢何必死缠烂打 2024-09-21 02:26:46
var gender = $('#genderLabel');
if (gender.text() == 'F') { gender.text('Female'); }
else { gender.text('Male'); }
var gender = $('#genderLabel');
if (gender.text() == 'F') { gender.text('Female'); }
else { gender.text('Male'); }
绿萝 2024-09-21 02:26:46

在 jQuery 1.4+ 中,您可以将函数传递给 .text()像这样:

$("#genderLabel").text(function(i, t) {
   return t == 'F' ? 'Female' : t == 'M' ? 'Male' : t;
});

这会将 F 转换为 Female,将 M 转换为 Male 并保留其他任何内容,以防出现新的性别过来 :)

In jQuery 1.4+ you can pass a function to .text() like this:

$("#genderLabel").text(function(i, t) {
   return t == 'F' ? 'Female' : t == 'M' ? 'Male' : t;
});

This converts F to Female, M to Male and leaves anything else alone, in case new genders come up :)

青芜 2024-09-21 02:26:46

将每个 value 替换为 text()

if($(this).text()=='F'){
   $(this).text('Female');
}
if($(this).text()=='M'){
    $(this).text('Male');
}

以及其他一些内容

replace every value with text()

if($(this).text()=='F'){
   $(this).text('Female');
}
if($(this).text()=='M'){
    $(this).text('Male');
}

Among a few other things

勿挽旧人 2024-09-21 02:26:46

要以这种方式使用 .html(),您需要传递一个 function 作为返回所需值的参数。

尝试一下: http://jsfiddle.net/FDZBJ/

$("#genderLabel").html(function(i,html) {
                           html = $.trim(html);
                           if(html == 'F') return 'Female';
                           if(html == 'M') return 'Male';
                           return html;
                       });

To use .html() that way, you need to pass a function as the argument that returns the value you want.

Try it out: http://jsfiddle.net/FDZBJ/

$("#genderLabel").html(function(i,html) {
                           html = $.trim(html);
                           if(html == 'F') return 'Female';
                           if(html == 'M') return 'Male';
                           return html;
                       });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文