ExtJS单选按钮值检索

时间:2017-09-25 09:14:41

标签: extjs

我需要检索选中的单选按钮的值,并将其作为参数发送到表单。我写了下面的代码块,但每次只传递第一个值。有人可以帮忙。

Ext.onReady(function() {
Ext.getCmp('myButton').setHandler(function(){

    Ext.MessageBox.show({
        title:    'Hello', 
        msg:      'Please choose one? <br></br> Select: '+
        '<input type="radio" name="newrate" value="Yes" checked /> Yes'+
        '<input type="radio" name="newrate" value="No" /> No',
        buttons:  Ext.MessageBox.OKCANCEL,
        fn: function(btn) {
            if( btn == 'ok') {
                alert(Ext.get('newrate').getValue());
                    var form = Ext.getCmp("MyForm").getForm();
                    form.getEl().dom.action="${pageContext.request.contextPath}/peer.online.MyForm.grid.action?mode=StornoDX&value="+Ext.get('newrate').getValue();
                    form.getEl().dom.method="POST";
                    form.getEl().dom.submit();
                }
        }
    });
});
});

1 个答案:

答案 0 :(得分:0)

首先,它的不是Extjs单选按钮Ext.get()用于获取带有id的html元素作为输入。在您的情况下,其一个html元素,名称为"newrate"。因此,您可以使用( For Browser&gt; IE8 document.querySelector()获取已检查单选按钮的值,如下所示:

document.querySelector('[name=newrate]:checked').value

示例工作代码适用于浏览器&gt; IE8

&#13;
&#13;
Ext.application({
      name: 'Fiddle',

      launch: function() {
      Ext.MessageBox.show({
        title:    'Hello', 
        msg:      'Please choose one? <br></br> Select: '+
        '<input type="radio" name="newrate" value="Yes" checked /> Yes'+
        '<input type="radio" name="newrate" value="No" /> No',
        buttons:  Ext.MessageBox.OKCANCEL,
        fn: function(btn) {
            if( btn == 'ok') {
                alert(document.querySelector('[name=newrate]:checked').value);
                  
                }
        }
    });
        }
      });
&#13;
<link rel="stylesheet" href="https://cdn.sencha.com/ext/gpl/4.1.1/resources/css/ext-all.css">
<script type="text/javascript"  src="https://cdn.sencha.com/ext/gpl/4.1.1/ext-all-debug.js"></script>
&#13;
&#13;
&#13;

IE8或更低浏览器不支持:checked CSS3选择器。因此,我们需要遍历所有单选按钮并找到已选中的单选按钮并获取其值:

var buttons = document.getElementsByName("newrate");
   for(var i = 0; i < buttons.length; i++) {         
      if(buttons[i].checked) {
         alert(buttons[i].value);
         break;
      }
   }

示例工作代码适用于所有浏览器

&#13;
&#13;
Ext.application({
      name: 'Fiddle',

      launch: function() {
      Ext.MessageBox.show({
        title:    'Hello', 
        msg:      'Please choose one? <br></br> Select: '+
        '<input type="radio" name="newrate" value="Yes" checked /> Yes'+
        '<input type="radio" name="newrate" value="No" /> No',
        buttons:  Ext.MessageBox.OKCANCEL,
        fn: function(btn) {
            if( btn == 'ok') {
var buttons = document.getElementsByName("newrate");
   for(var i = 0; i < buttons.length; i++) {         
      if(buttons[i].checked) {
         alert(buttons[i].value);
         break;
      }
   }                      
                }
        }
    });
        }
      });
&#13;
<link rel="stylesheet" href="https://cdn.sencha.com/ext/gpl/4.1.1/resources/css/ext-all.css">
<script type="text/javascript"  src="https://cdn.sencha.com/ext/gpl/4.1.1/ext-all-debug.js"></script>
&#13;
&#13;
&#13;