javascript - 使用 jQuery 单击输入单选按钮时更改跨度的文本值

标签 javascript jquery

我想要文字“评分!” (62)' 在 'rate' 类中更改为 'Rate! (63)',当用户单击其中一个输入单选按钮时。

困难在于我有很多“星星”div,因此这种更改必须仅发生在封闭“星星”div 的下一个跨度后代中。

   <div class="stars float-left">
    <div class="rating" style="width:99%"></div>
    <p><input type="radio" name="rating" id="star5" value="5"><label for="star5"></label><input type="radio" name="rating" id="star4" value="4"><label for="star4"></label><input type="radio" name="rating" id="star3" value="3"><label for="star3"></label><input type="radio" name="rating" id="star2" value="2"><label for="star2"></label><input type="radio" name="rating" id="star1" value="1"><label for="star1"></label>
    </p>
    <span class="rate">Rate! (62)</span>
</div>

我是 jQuery 新手,这是我到目前为止想到的,但它不起作用。我想我在路上混淆了一些事情:

('.rating input:radio').click(function() {
    var str_rate = $(obj).closest('span').next().find('.rate');
    //TODO: parse the current value and add 1 to it
    $(str_rate).html(str);
}

有什么我完全想念的吗?

更新: This jsfiddle显示了到目前为止考虑答案的进度,但仍然无法正常工作。

最佳答案

你的问题是你的选择器找到span,将其更改为:

$(obj).parents('.stars').find('.rate');

因此,我们获取父 stars 元素,然后获取关联的 .rate span

另外:您的 click 函数开头缺少 $。和 ); 从末尾开始。

关于javascript - 使用 jQuery 单击输入单选按钮时更改跨度的文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28139841/

相关文章:

javascript - 在鼠标悬停在 Canvas 上显示文本

javascript - 产生多个 Promise

javascript - JS图片没有变化,错误: anonymous function

jQuery:悬停时将内容放入 DIV

javascript - 从 margin-left :-9999px to the page? 移动元素时如何进行平滑过渡

javascript - 除非用户坚持小写,否则在用户键入时将每个单词的首字母大写

javascript - 如何在最右边的文本框中禁用非整数输入?

javascript - SharePoint <button> 元素执行不需要的页面重新加载

jquery - 按钮禁用和启用在 Jquery 中不起作用

javascript - Jquery if 条件,如果字符串格式不正确,将调用警报