简体   繁体   English

根据单选按钮值更改提交按钮文本

[英]Change submit button text based on radio-button value

How to change submit button text based on which radio-button is active? 如何基于哪个单选按钮处于活动状态来更改提交按钮文本?

Here is my code, but it does not work. 这是我的代码,但是不起作用。 It changes text only once. 它仅更改一次文本。

I have two radio-buttons: 我有两个单选按钮:

<input type="radio" name="build-team" class="choose" value="build" /> Yes
<input type="radio" name="build-team" class="choose" value="show" /> No
<button class="show-result" data-chooseyes="Yes" data-chooseno="No">Yes</button>

And I have script: 我有脚本:

$(document).on('click', '.choose', function() {
    var target = $('.show-result');
    if ($(this).attr('checked') == 'checked') {
      target.html(target.data('chooseyes'));
    }
    else {
      target.html(target.data('chooseno'));
    }
})

JSFiddle example JSFiddle示例

 $(document).on('click', '.choose', function() { var target = $('.show-result'); target.html($(this).val()); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="radio" name="build-team" class="choose" value="Yes" /> Yes <input type="radio" name="build-team" class="choose" value="No" /> No <button class="show-result" data-chooseyes="Yes" data-chooseno="No">Yes</button> 

 $('.opt').click(function(){ $('#button').html($(this).val()); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="opt" name="opt" type="radio" value="Yes" checked="checked">Yes <input class="opt" name="opt" type="radio" value="No">No <button id="button" type="submit">Yes</button> 

This is the easiest answer, no added attributes, only clean HTML + jQuery, it is also important to realize that there must always be a radio selected/checked by default, if not, you would have to validate the field and validating a radio is not cool haha :) have a nice day! 这是最简单的答案,不添加任何属性,仅使用干净的HTML + jQuery,认识到默认情况下始终必须选中/选中某个收音机是非常重要的,否则,您必须验证该字段并验证收音机是否为不酷哈哈:)祝你有美好的一天!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM