简体   繁体   English

使用jQuery在页面加载时获取单选按钮的默认值

[英]Getting default value of radio button upon page load using jQuery

I have two radio buttons whom I set to default (first selected value radio button is the default one upon load) like following: 我有两个单选按钮,我将它们设置为默认按钮(第一个选定值单选按钮是加载时的默认按钮),如下所示:

  var listing_type;
  var shipping_location;
  $(":radio[name='type'][value='1']").attr('checked', 'checked');
  $(":radio[name='shipping'][value='1']").attr('checked', 'checked');

When the user clicks on some of the values from radio buttons, the values are caught like following: 当用户单击单选按钮中的某些值时,将按如下所示捕获值:

  $('input:radio[name=type]').click(function () {
            listing_type = $(this).val();
        });
        $('input:radio[name=shipping]').click(function () {
            shipping_location = $(this).val();
        });

This 2nd part of the code works just fine, when the user clicks on some of the radio buttons, values are passed into my MVC Action just correctly. 代码的第二部分工作正常,当用户单击某些单选按钮时,值会正确传递到我的MVC Action中。

However, I'm unable to catch the value if the user doesn't clicks anything on the radio buttons (ie leaves them as default as they are upon page load) and clicks "Search" Button... 但是,如果用户未单击单选按钮上的任何内容(即将其保留为页面加载时的默认值),然后单击“搜索”按钮,则无法捕获该值。

The values in my MVC Action are always null for some reason, even though they aren't supposed to be. 由于某些原因,我的“ MVC操作”中的值始终为null,即使不应这样做。 Here is how I'm passing the values and the here is the C# code from the Action: 这是我传递值的方式,这是Action中的C#代码:

  $.post("/Search/Index", { keywords: $('.txtSearch').val(), type: /*  listing_type */ <=> $('input[name=type]:checked').val(), location: $('input[name=shipping]:checked').val() <=> /*shipping_location*/ }, StartLoading())
                           .done(function (data) {
                               StopLoading();
                               var brands = $('<table />').append(data).find('#tableProducts').html();
                               $('#tableProducts').html(brands);
                               $('#tableProducts thead').show();
                           });
            }

And this is the Action: 这是动作:

 public ActionResult Index(string keywords, string type, string location)
        {
// If the user doesn't interacts with the radio buttons and just enters the search term (ie. keywords) the type and location variables are null).  
       if ((keywords != null && keywords != "")  && (type != null && type != "") && (location != null && location!=""))
            {
                // do something here.... 
            }
            return View();
        }

So now my question is: 所以现在我的问题是:

How can I pass the default values from the radio buttons if the user doesn't interacts with them (ie passing the default values I've set upon page load)??? 如果用户不与单选按钮交互,我该如何传递默认值(即传递我在页面加载时设置的默认值)?

Give this a try: 试试看:

$('input:radio[name="type"]').filter('[value="1"]').attr('checked', true);
$('input:radio[name="shipping"]').filter('[value="1"]').attr('checked', true);

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

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