更改单选按钮检查按钮的文本

时间:2014-04-01 16:44:40

标签: javascript

我试图根据正在检查的单选按钮更改按钮内显示的文字。

我的代码是:

<html>
<head>
<title></title>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
$("input[type=radio]").click(function(){
if(document.getElementById('segment1').checked) {
 alert('Yes');
 document.getElementById('sendbutton').value="Proceed";
}
else if(document.getElementById('segment2').checked) {
 alert('No');
 document.getElementById('sendbutton').value="No";
}
});

</script>
</head>
<body>
<form action="composemail" enctype= "multipart/form-data" method="post" name="f1">
<input type="text" placeholder="Email TO" name="mailreciever">
<input type="text"  placeholder="Email SUBJECT" name="mailsubject">
<textarea placeholder="Message"  name="messagearea"></textarea>
<input type="file" name="uploadfile" id="uploadfile"/>

<input type="radio" name="radio-view"  data-icon="segment-titlestyle-segonly"  id="segment1" value="Yes"/> 
<label for="segment1"  id="controls">
<span class="ui-btn-text-controls">Yes</span> 
</label> 

<input type="radio" name="radio-view" data-icon="segment-titlestyle-segonly" id="segment2" value="No" checked="checked"/> 
<label for="segment2" id="controls">
<span class="ui-btn-text-controls">No</span>
</label> 
<input type="submit" value="send" name="sendbutton" id="sendbutton"/>
</form>
</body>
</html>

我根据答案找到了我的代码但是它仍然没有用。请帮助

2 个答案:

答案 0 :(得分:1)

尝试.value而不是.text

if(document.getElementById('segment1').checked) {
     alert('Yes');
     document.getElementById('sendbutton').value="Proceed";
}
else if(document.getElementById('segment2').checked) {
     alert('No');
     document.getElementById('sendbutton').value="No";
}

答案 1 :(得分:0)

如果您要使用jQuery,请使用jQuery:

$("input[type=radio]").click(function () {
    $('#sendbutton').val(($('input[name="radio-view"]:checked').val() == 'Yes') ? 'Proceed' : 'No');
});

<强> jsFiddle example